• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to connect multiple combo boxes?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, can someone help me to figure out how to connect multiple combo boxes in Java?

The thing is that I need like 5 combo boxes and every time you select something in the previous combo box, the list in the next combo box gets changed to the condition set in the previous combo box.

I found this example on then net, and implemented it in my program. Now this works just fine, but this example connects just 2 combo boxes by the means of if/else in that last method. Now I need to be able to connect more combo boxes in the similar way, but if i did it like this then I would end up writing a lot of if/else statements.

Please I need some help, this problem is bugging me for quite some time, and atm I am getting really desperate.


 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than the if/else construct you've shown, you could use a switch statement based on the previous combo box' selected item index using method getSelectedIndex(). Looking at the methods for JComboBox may give you other ideas.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use if/else statments, just use a separate ActionListener for each combo box.

Don't use if/else statements within the ActionListener to determine which item was selected. You can use an approach like this:





And don't use a "null layout". Swing was designed to be used with layout managers.
 
Alex Cage
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply's, they really helped me to learn something new. Rob you really helped a lot, it just I still can not figure out how to make 3 combo boxes or more to be connected. I mean I can now connect two combo boxes in a proper way. But with the third combo box, I want it to be dependent on the second combo box which is then dependent on the first combo box, now I ask you is there a way to do this without using a ton of if/else or action listeners. As far as I searched on the net, these combo boxes are called dynamic or cascading combo boxes, but I only find examples for JSP or Ajax, can't really figure out how to make this work in Java. So if someone has an idea I would gladly accept their help.

Thank you for your help people, much appreciated.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic