aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JComboBox with DefaultComboBoxModel doesn't allow to add or remove Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox with DefaultComboBoxModel doesn Watch "JComboBox with DefaultComboBoxModel doesn New topic
Author

JComboBox with DefaultComboBoxModel doesn't allow to add or remove

Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

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();





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!
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Help
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Please don't litter your post with bold sections, it makes it hard to read.

I'm not sure why those methods aren't working but it may be because you are trying to alter the model whilst you are in the process of entering data into the JComboBox.
BTW Why are you using a KeyListener, can you explain exactly what are you trying to achieve that can't be done via one of the standard JComboBox listeners?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Help

Being impatient won't get you an answer any quicker and in fact will put some people off.
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Tony Docherty wrote:Please don't litter your post with bold sections, it makes it hard to read.

I'm not sure why those methods aren't working but it may be because you are trying to alter the model whilst you are in the process of entering data into the JComboBox.
BTW Why are you using a KeyListener, can you explain exactly what are you trying to achieve that can't be done via one of the standard JComboBox listeners?



I am trying to take the input from the ComboBox only when the user presses the Enter key. I had problems getting the KeyListener working too, because the Glazedlist(Autocomplete) already had an action listener for Enter key and I had to change the library event for Enter Key.

Sorry for the "Help" post but I have no idea how to delete a post, I made a mistake when I wrote it.

Thanks for your interest!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Can't you just add an ActionListener which will be called when the user presses Enter.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Oh, and welcome to the ranch.
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Tony Docherty wrote:Can't you just add an ActionListener which will be called when the user presses Enter.


But you still need to add a KeyEvent... no?

This is the way I have have been taught to us the Listeners. I will try to look and find some examples, to understand what you mean and then change it maybe it will fix my problem.

Thanks
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Tony Docherty wrote:Oh, and welcome to the ranch.


Q: As I can see my question is marked as answered. how can you change the status of a question?

As I can see this is a nice place from where you can learn a lot. I use this website every day, but is the first time when I have a problem and I can not fix it by myself.

Thanks for taking you time and being so nice!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
But you still need to add a KeyEvent... no?

No. Handling key events is a fairly low level approach and should be avoided wherever possible.

I suggest you google for something like "java actionlistener jcombobox tutorial"

Q: As I can see my question is marked as answered. how can you change the status of a question?

Sorry I can't help you with that, you'll need to contact one of the mods.
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 156
again

1. no idea whats your goal, (then) nor how could be done with GlazeList,

2. add DocumentListener to the derived Editor, insted of KeyListener,

3. you can use KeyListener only for derived JList from JComboBox, but this is low_level feature

4. is JComboBox#setEditable(true),

5. and another 3-4 question based on == for better help sooner post an SSCCE
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

m Korbel wrote:again

1. no idea whats your goal, (then) nor how could be done with GlazeList,

2. add DocumentListener to the derived Editor, insted of KeyListener,

3. you can use KeyListener only for derived JList from JComboBox, but this is low_level feature

4. is JComboBox#setEditable(true),

5. and another 3-4 question based on == for better help sooner post an SSCCE



1. my goal is to be able to add and remove from the comboBox items.

2. i will do that

3. thanks

4. the combobox is setEditable to (true);

When I attached the GlazeList library, I had to change the ComboBox to mutable because I was getting an error when I was trying to remove or add. I have done that by setting the DefaultComboBoxModel to my ComboBox.

Now what I have to do is to remove everything from the comboBox and add the new Items. Now I have to use the model.removeAllElements(); which is not working.

Thanks for your help! I appreciate!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
2. i will do that

No, don't do that. You are making things way more complicated than they need to be. Ive already told you just need to add an ActionListener.
In the ActionListener's actionPerformed method check if the event is a change command or an edit command by checking the event's command string and if it is an edit get the selected item from the JComboBox and then add it to the JComboBox using the addItem() method. Something like:
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Tony Docherty wrote:
2. i will do that

No, don't do that. You are making things way more complicated than they need to be. Ive already told you just need to add an ActionListener.
In the ActionListener's actionPerformed method check if the event is a change command or an edit command by checking the event's command string and if it is an edit get the selected item from the JComboBox and then add it to the JComboBox using the addItem() method. Something like:



I will give it a try. Thanks a lot
Mihai Rotariu
Greenhorn

Joined: Nov 21, 2012
Posts: 8

Solution:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox with DefaultComboBoxModel doesn't allow to add or remove