Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListener on JComboBox fires subsequently on next all Comboxes

 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic