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


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

JComboBox with DefaultComboBoxModel doesn't allow to add or remove

Started byRotariu Mihai <rotariu.mihai7@gmail.com>
First post2012-11-21 04:27 -0800
Last post2012-11-21 20:45 -0500
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#19833 — JComboBox with DefaultComboBoxModel doesn't allow to add or remove

FromRotariu Mihai <rotariu.mihai7@gmail.com>
Date2012-11-21 04:27 -0800
SubjectJComboBox with DefaultComboBoxModel doesn't allow to add or remove
Message-ID<6b564835-30c6-4fd7-86fb-d89693675be4@googlegroups.com>
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!

[toc] | [next] | [standalone]


#19835

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-21 06:31 -0800
Message-ID<3dppa8tt2iudu7uqm4vbem28021g3psf5b@4ax.com>
In reply to#19833
On Wed, 21 Nov 2012 04:27:15 -0800 (PST), Rotariu Mihai
<rotariu.mihai7@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>model.removeAllElements(); and model.addElement(s); is not workin

What are the symptoms it is not working?

Perhaps you need a manual repaint after you have done your changes.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish 
as couch potatoes who hire others to go to the gym for them. 

[toc] | [prev] | [next] | [standalone]


#19836

Frommarkspace <-@.>
Date2012-11-21 10:41 -0800
Message-ID<k8j785$bia$1@dont-email.me>
In reply to#19833
On 11/21/2012 4:27 AM, Rotariu Mihai wrote:
> 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();


Why do you have to do that?  If you're using the right kind of model, 
shouldn't removeAllItems() call the model appropriately?

Please, please, please show us an SSCCE -- a short, self-contained 
compilable example.  We can't really tell what is going on unless you 
provide one.

sscce.org

[toc] | [prev] | [next] | [standalone]


#19848

FromRotariu Mihai <rotariu.mihai7@gmail.com>
Date2012-11-22 04:03 -0800
Message-ID<15fd295b-fc0f-4403-b47f-9842984febf5@googlegroups.com>
In reply to#19836
On Wednesday, 21 November 2012 19:41:10 UTC+1, markspace  wrote:
> On 11/21/2012 4:27 AM, Rotariu Mihai wrote:
> 
> > 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();
> 
> 
> 
> 
> 
> Why do you have to do that?  If you're using the right kind of model, 
> 
> shouldn't removeAllItems() call the model appropriately?
> 
> 
> 
> Please, please, please show us an SSCCE -- a short, self-contained 
> 
> compilable example.  We can't really tell what is going on unless you 
> 
> provide one.
> 
> 
> 
> 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

[toc] | [prev] | [next] | [standalone]


#19854

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-11-22 10:37 -0800
Message-ID<k8lrdm$vqg$1@dont-email.me>
In reply to#19848
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

[toc] | [prev] | [next] | [standalone]


#19840

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-11-21 20:45 -0500
Message-ID<nospam-F59273.20454821112012@news.aioe.org>
In reply to#19833
In article <6b564835-30c6-4fd7-86fb-d89693675be4@googlegroups.com>,
 Rotariu Mihai <rotariu.mihai7@gmail.com> wrote:

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

Cross-posted here: <http://stackoverflow.com/q/13491053/230513>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web