File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to connect multiple combo boxes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to connect multiple combo boxes?" Watch "How to connect multiple combo boxes?" New topic

How to connect multiple combo boxes?

Alex Cage

Joined: May 21, 2011
Posts: 2
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.

Greg Brannon

Joined: Oct 24, 2010
Posts: 563
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.

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2451
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

Joined: May 21, 2011
Posts: 2
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.
I agree. Here's the link:
subject: How to connect multiple combo boxes?
It's not a secret anymore!