Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ActionListener on JComboBox fires subsequently on next all Comboxes

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
>
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
>
 
reply
    Bookmark Topic Watch Topic
  • New Topic