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


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

JComboBox with DefaultComboBoxModel doesn't allow to add or remove

Newsgroups comp.lang.java.programmer
Date 2012-11-21 04:27 -0800
Message-ID <6b564835-30c6-4fd7-86fb-d89693675be4@googlegroups.com> (permalink)
Subject JComboBox with DefaultComboBoxModel doesn't allow to add or remove
From Rotariu Mihai <rotariu.mihai7@gmail.com>

Show all headers | View raw


This is my first post and I think I am doing it right. 

I have a program that takes the user input from a AutoComplete jComboBox and then send's the input to be stored into a text file.(AutoComplete is done using the library glazedlists_java15/1.8.0). 

After using the Autocomplet feature I had to set the jComboBox to DefaultComboBoxModel. 

When the user presses the Enter key, the jComboBox should update the list with the new Item typed from the keyboard, so the user can see the last typed item in the jComboBox list. 

This is done by removing all the items from the jComboBox and then inserting them again from the text file. 

The problem is that before having the AutoComplete feature I could just say jComboBox1.removeAllItems(); but now because of the model I have to do it with model.removeAllElements(); 

view plaincopy to clipboardprint?
final javax.swing.JComboBox jComboBox1;  
    final DefaultComboBoxModel model = new DefaultComboBoxModel();  
    jComboBox1 = new javax.swing.JComboBox(model);  
    jComboBox1.setModel(model);  
  
    jComboBox1.getEditor().getEditorComponent().addKeyListener(  
            new KeyListener() {  
  
                @Override  
                public void keyPressed(KeyEvent e) {  
                    String ip = (String) jComboBox1.getEditor().getItem();  
                    int key = e.getKeyCode();  
                    if (key == KeyEvent.VK_ENTER) {  
  
                        if (ip == null || ip.trim().isEmpty()) {  
                            jComboBox1.setSelectedItem("Please insert a keyword!");  
                        } else {  
                            /* 
                             * Configuration is the class where I have the method that writes to the file and read from the text file 
                             */  
                            Configuration.WriteIP();  
  
                        }  
                        /* 
                         * here I remove the items from the comboBox 
                         */  
  
                        model.removeAllElements();  
  
                        /* 
                         * here I set the items from the text file(including the last typed) to the jComboBox 
                         */  
  
                        for (Object s : Configuration.getArrayss()) {  
                            model.addElement(s);  
                        }  
                        jComboBox1.setSelectedItem(ip);   
                        //jComboBox1.getEditor().getEditorComponent().requestFocus();  
                        //jComboBox1.addFocusListener(focusHandler);  
                    }  
  
                }  
  
                @Override  
                public void keyReleased(KeyEvent arg0) {  
                    // TODO Auto-generated method stub  
  
                }  
  
                @Override  
                public void keyTyped(KeyEvent arg0) {  
                    // TODO Auto-generated method stub  
  
                }  
            });  
  
}  




The problem is that model.removeAllElements(); and model.addElement(s); is not working so I can not update the jComboBox. 

Can you please help me find a solution. 

Thanks!

Back to comp.lang.java.programmer | Previous | NextNext 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