This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
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.
Joined: May 21, 2011
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.