• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Loading Values into JComboBox, depending on selection from another JComboBox

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic