Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • 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
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2600
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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???
 
Craig Cattell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain more about your problem.
 
Piet Souris
Rancher
Posts: 1296
28
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic