Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19854

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
Date 2012-11-22 10:37 -0800
Organization A noiseless patient Spider
Message-ID <k8lrdm$vqg$1@dont-email.me> (permalink)
References <6b564835-30c6-4fd7-86fb-d89693675be4@googlegroups.com> <k8j785$bia$1@dont-email.me> <15fd295b-fc0f-4403-b47f-9842984febf5@googlegroups.com>

Show all headers | View raw


On 11/22/2012 4:03 AM, Rotariu Mihai wrote:
>>
>> sscce.org
>
>
> I updated my post on another java forum where I have a very simple
> example why is not working. If you have the time and the patience
> please check
>
> http://stackoverflow.com/questions/13491053/jcombobox-with-defaultcomboboxmodel-doesnt-allow-to-add-or-remove

That doesn't really cut it as an SSCCE because it doesn't compile.  But 
I was intrigued enough to write one for you.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class test extends JPanel {
     static final String[] items = { 
"one","two","three","four","five","six" };

     private final Vector<String> vector = new Vector<>();
     private final JComboBox<String> box;

     public test() {
         box = new JComboBox<String>(vector);
         box.setEditable(true);
         for (String item: items)
             box.addItem(item);
         box.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 String item = (String)box.getSelectedItem();
                 // if item is not in list and item is not empty
                 if (!vector.contains(item) && !item.equals("")) {
                     box.addItem(item);
                     // attempt to blank entry field
                     Component c = box.getEditor().getEditorComponent();
                     if (c instanceof JTextComponent)
                         ((JTextComponent)c).setText("");
                 }
             }
         });

         JButton rem = new JButton("Remove All");
         rem.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 vector.removeAllElements();
                 Component c = box.getEditor().getEditorComponent();
                 if (c instanceof JTextComponent)
                     ((JTextComponent)c).setText("");
             }
         });

         add(box);
         add(rem);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame("test");
                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 f.pack();
                 f.setLocationRelativeTo(null);
                 f.setVisible(true);
             }
         });
     }
}

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

JComboBox with DefaultComboBoxModel doesn't allow to add or remove Rotariu Mihai <rotariu.mihai7@gmail.com> - 2012-11-21 04:27 -0800
  Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove Roedy Green <see_website@mindprod.com.invalid> - 2012-11-21 06:31 -0800
  Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove markspace <-@.> - 2012-11-21 10:41 -0800
    Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove Rotariu Mihai <rotariu.mihai7@gmail.com> - 2012-11-22 04:03 -0800
      Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove Knute Johnson <nospam@knutejohnson.com> - 2012-11-22 10:37 -0800
  Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove "John B. Matthews" <nospam@nospam.invalid> - 2012-11-21 20:45 -0500

csiph-web