This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I have a file that stores 16 JComboBox objects. The items of some of these objects depend on other. (Like first you select region, then according to region, the items of circle combobox would be displayed and so on.) So i want to ask,
1. Which listener to use for JComboBoxes?
2. I implement the listeners in the same class in which i define objects and store them in a file. Now what if i retrieve those objects from the file in another class and use them, will the listeners be invoked when i perform actions on comboboxes?
1) ActionListener and ItemListener are probably the most common. work out which is best for you
2) sounds like you want your listener in its own class.
If so, you'd create an instance of that class and add it as each comboBox's listener,
then, in the listener class, you'd use getSource() to ID the source. If you have 16 sources,
this code will get ugly.
Joined: May 22, 2012
Ok.. lets say i have cb as my JcomboBox objects and i am saving them in a file objects.ser. Now contents of cb depend on selected item of cb and contents of cb depend on selected item of cb. I implement cb.addActionListener(this) and cb.addActionListener(this) in the same class.
1. It generates error whenever i perform any action on cb as it changes the contents of cb and cb itself has an action listener. (that's what i think as the reason of error as shown by Eclipse IDE). How can i resolve this issue?
2. If i get the comboboxes stored in objects.ser in another class and use them, would the listeners still work? (as i am using the same objects)
Joined: Jun 09, 2003
> It generates error whenever i perform any action on cb
that doesn't tell us much.
this is (generally) how it works
1) select item in cb
2) the listener code for cb creates a comboBoxModel of items, based on the selection
3) the model for cb is set from the listener, and cb will now display the items relative to the cb selected item
if the above creates problems with the cb listener, remove cb's listener before (3), then add it back after (3)
Joined: May 22, 2012
Thanks a lot, Michael. Your reply helped a lot; removed all redundant code form my program
Thanks again.... o/\o