• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox event triggers another JComboBox

 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two JComboBox say 'A' and 'B'. I want event change(item selection) on 'A' to trigger automatic change on another JComboBox 'B'. Items(contents) in 'B' are dynamically read from somewhere. I looked API but didn't find suitable methods. I am sure there must be a way to do the job, but couldn't figure it out. Can anyone please give me suggestion or any code snippets would be appreciated.
 
Eric Snell
Greenhorn
Posts: 28
Java Mac MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fei long:
I have two JComboBox say 'A' and 'B'. I want event change(item selection) on 'A' to trigger automatic change on another JComboBox 'B'. Items(contents) in 'B' are dynamically read from somewhere. I looked API but didn't find suitable methods. I am sure there must be a way to do the job, but couldn't figure it out. Can anyone please give me suggestion or any code snippets would be appreciated.


Add an ActionListener to combobox A. When it's actionPerformed method is invoked you can call getSelectedItem() on it and select an item in combobox B based on that.
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Eric.
At moment, I have Combo_A, Combo_B and List_C, and Combo A and B are using ItemListener(). When I change Combo_A, Combo_B is using removeAllItems() to remove all existing contents first then trying to add new contents. But this causes event trigger on Combo_B and Comb_B is not updated properly.
Below is my basic code structue, and please revise me if I'm wrong.
 
Eric Snell
Greenhorn
Posts: 28
Java Mac MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's a little hard to answer without knowing exactly what you're trying to accomplish.

You can add a method to your ComboBoxB model that makes the necessary changes without firing an update event until all processing is completed. This would allow you to make multiple changes to the model and then fire 1 event.

pseudo code:

I prefer to make changes to my models by calling the model itself, as opposed to calling the GUI widget.

Hope that helps.
[ June 15, 2004: Message edited by: Eric Snell ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!