wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox" Watch "JComboBox" New topic


Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
hi there,
I have used JComboBox in one of my programs, registered and implemented the ItemStateChangeListener for the same. It worked fine when I selected the combobox with mouse and keyboard. But when I try to select it through my program, it gets selected but the event doesnt get fired.
Would n e body tell me where I went wrong?
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

From the API Documentation for ItemListener:

void itemStateChanged(ItemEvent e)
Invoked when an item has been selected or deselected by the user.

Sounds like it is working correctly to me.

[How To Ask Questions On JavaRanch]
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
hi joe,
Thanx for ur reply.
The actual problem was, the event was not fired when the jcombobox was selected through my code. [ jCombobox.setSelected(true); //although the combox gets selected the event was not being fired. ]
Basically, I wanted to fire the event without using the mouse & keyboard manually. It should be performed through my code itself.
Hope u got me this time.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome tot he Ranch sai,
from the JComboBox API:
protected void fireItemStateChanged(ItemEvent e)
Notifies all listeners that have registered interest for notification on this event type.

SCJP2. Please Indent your code using UBB Code
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
I'm just guessing here, but it sounds to me like you're trying to fire an event with code? I'm sure there is a way to do that, but - again, I'm guessing - it sounds like just setting the combo-box to a new value isn't going to generate an actionEvent or stateChange. I thought those events were actually 'fired' through the operating system, and then handed off to our programs to be handled. If that's the case, then just changing it in code doesn't fire the event. I'll bet there is a way to - using code - simulate an event to be fired, so your code can handle it just as if a user made the change. In fact, I'm pretty sure that can be done. Anyone know how?

David Crossett
-nothing important to say, but learnin' plenty-
I agree. Here's the link: http://aspose.com/file-tools
subject: JComboBox
It's not a secret anymore!