Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox selection 1

Status
Not open for further replies.

kohinoor2007

Programmer
Mar 21, 2007
69
DE
Hi guys,

Iam new to Listeners under Swing.Is working on a task under swing & came across the following problem.

Have a comboBox(Say testCombo) filled with some string values.When the user selects one item from thge popup menu or when he presses "Enter" I want to do some action.

Tried adding both the ItemListener & ActionListener to it,but then,

the events are generated even while the user is moving through items in the displayed popup menu or when I add I add some items in the combobox.Thats not what I want..

I want to do some action only when the user selects an item from the comboxbox.(Iam coming from the MFC world.In MFC, could achieve it on a comboclose up event )

How can I achieve this under swing...

Thanks..
 
Just need an ActionListener (not the ItemListener). This is then fired when an item is selected.

Tim
 
Yes,That I have already said in my post that I have used the actionListener.But dont want it to get fired when Iam adding something into the combobox, which it does.....

want the listener to be called when its getting selected only...
 
You could either remove the listener before you add new items, then add it back after, or use a boolean which you set to true for the duration of the item addition. Your listener code would check the value of this boolean and do nothing if set.

Tim
 
I tried both the listeners.Is it the only way under swing to fulfill my requirements-providing flags & removing listeners...

>>the events are generated even while the user is moving through items in the displayed popup menu .

That will be still there. Cant we make java user actions custom made.. as I said -an event to be called on comboclose up....

This is really a surprise for me!!!
 
Swing is often unwieldy. There are many aspects of it which take some effort to control. Some of the events do seem to be a little coarse-grained at times. This is one of those.

There is an alternative, offered by IBM and used in their Eclipse IDE. It's called SWT and it may be better for you.


Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top