Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading Values into JComboBox, depending on selection from another JComboBox  RSS feed

 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, hope you're well :-)

just having a little problem, I know how to update a textField with Actionlistener once the GUI has loaded using "setText"

So I have 2 JCombobox's........
but what I want to do is update the entire contents of the 2nd JComboBox depends on what is selected in the 1st JComboBox...once the gui has loaded to contain an array of values depending on which

is there a similiar function liek "setText" for JComboBox's....like there is for the TextField and setText....

how would one go about doing this, any point in the right direction would be much appreciated

....

Example scenario.

I have a JCombobox A with Countries.
I want to have the cities for that country in JComboBox B when you select it in ComboBox A.

Thanks :-)
Gus.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gus Parker wrote:
is there a similiar function liek "setText" for JComboBox's....like there is for the TextField and setText....


JComboBox#setModel(new DefaultComboBoxModel(Object[] items));
 
Sanjeev Debnath
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To implement the example scenario,

you need to add the ActionListner on you Country ComboBox and handle the event within this listener.
For example, whenever user chooses any country, query your DB to get the cities for the selected country and update the model of
the cities combo box so that the City ComboBox will display the updated data.

Refer to this link for more information: How to Use Combo Boxes.

And to set any item selected in the combo box, check the below examples:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!