File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes EventHandling JcomboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "EventHandling JcomboBox" Watch "EventHandling JcomboBox" New topic
Author

EventHandling JcomboBox

Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62

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?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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.
Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62
Ok.. lets say i have cb[16] as my JcomboBox objects and i am saving them in a file objects.ser. Now contents of cb[1] depend on selected item of cb[0] and contents of cb[2] depend on selected item of cb[1]. I implement cb[0].addActionListener(this) and cb[1].addActionListener(this) in the same class.
Now,
1. It generates error whenever i perform any action on cb[0] as it changes the contents of cb[1] and cb[1] 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)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> It generates error whenever i perform any action on cb[0]

that doesn't tell us much.

this is (generally) how it works
1) select item in cb[0]
2) the listener code for cb[0] creates a comboBoxModel of items, based on the selection
3) the model for cb[1] is set from the listener, and cb[1] will now display the items relative to the cb[0] selected item

if the above creates problems with the cb[1] listener, remove cb[1]'s listener before (3), then add it back after (3)
Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62
Thanks a lot, Michael. Your reply helped a lot; removed all redundant code form my program
Thanks again.... o/\o
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EventHandling JcomboBox
 
Similar Threads
Is there any means to check the number of objects created when a java prgram runs.
object references
Class definition
the importance of wrappers
need help with JPanel, liseners, and referencing vectors