• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JComboBox event triggers another JComboBox

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
Mac MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic