• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to fire an initial event to JComboBox (ItemListener)?

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I add an itemListener to the JComboBox,
It isn't called when its container/itself is first setup,
How can I call it once when the "JDialog" constructor is called?
Thanks
Jack
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ItemListener is triggered when the item selection changes. Check out the API for any method which will let you programatically change the selection. Check if this fires the event.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:ItemListener is triggered when the item selection changes. Check out the API for any method which will let you programatically change the selection. Check if this fires the event.





Hello,
the itemStateChanged event is never called upon, when the setSelectedIndex(0) is set after it
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacky Luk wrote:How can I call it once when the "JDialog" constructor is called?



You need to call it at the end of your constructor.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:

Jacky Luk wrote:How can I call it once when the "JDialog" constructor is called?



You need to call it at the end of your constructor.



Like this?, but still doesn't select the first item... strange.
 
Rancher
Posts: 3324
32
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your SSCCE???

If you say something doesn't work, then prove it. Post the SSCCE that you used to test the concept!!!

How hard is this to understand???
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree if you invoke setSelectedIndex() on the JComboBox it does not fire the event - it's quite easy to see just do a print from the itemStateChanged() method in the ItemListener or set a debug point in there.

I'd find this useful as I have an underlying data structure which I update when the combobox item is selected - but it needs initialising.

Obviously, I can do that by hand but it would be better/more consistent to do it via the event handler
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain more about your problem.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Craig Cattell wrote:
(...)
Obviously, I can do that by hand but it would be better/more consistent to do it via the event handler


An easy way to achieve this is (cb is my combobox)

if 0 should be the initial index.
The ItemListener is then activated.

Greetz,
Piet
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic