aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ActionListener on JComboBox fires subsequently on next all Comboxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListener on JComboBox fires subsequently on next all Comboxes" Watch "ActionListener on JComboBox fires subsequently on next all Comboxes" New topic
Author

ActionListener on JComboBox fires subsequently on next all Comboxes

Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Hello,
I am facing a problem with JComboBox ActionListener. I have 3 comboBox and the logic which I am expecting is -
User select Item from 1st combobox, then only 2nd comboBox will be populated.
then user selects 2nd combobox, then 3rd comboBox will gets populated.
...
and so on.

I have made only 3 combobox here for this post. in an actual scenario I have around 8-10 combobox'x each having nearly 10 items.


The problem I am facing here is very tedious. All the listeners in all combobox's fired.
I tried removing and then re-adding action listener, but that gives me compilation issue.
1st issue - Cannot refer to a non-final variable listener inside an inner class defined in a different method
to correct this, if I make ActionListener definition as final, then another compilation issue comes - The local variable listener may not have been initialized.

Please help me in resolving this.
Code snippet -



Thanks In Advance,
Sharad
>
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- all data for JComponents are stored in Model

- put data to the DefaultComboBoxModel,

- from ItemListener (suitable, better listener for JComboBox) or ActionListener to switch betweens model

- code example by Rob or Darryl from old.sun.forums57 (little bit modified)

Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Thanks. This logic works for me.
I am just posting the code differences I made to make it work w.r.t the original example.

old code snippet



Changed code snippet
>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ActionListener on JComboBox fires subsequently on next all Comboxes