This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to fire an initial event to JComboBox (ItemListener)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to fire an initial event to JComboBox (ItemListener)?" Watch "How to fire an initial event to JComboBox (ItemListener)?" New topic
Author

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

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 634
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
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10745
    
    9

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 634
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10745
    
    9

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

Joined: Aug 02, 2012
Posts: 634
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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2373
    
    8
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???
Craig Cattell
Greenhorn

Joined: Jun 08, 2015
Posts: 1
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43980
    
  33
Welcome to the Ranch

Please explain more about your problem.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 915
    
  11
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to fire an initial event to JComboBox (ItemListener)?
 
It's not a secret anymore!