This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EventHandling JcomboBox

 
Paras Ahuja
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic