ItemListener.itemStateChanged(ItemEvent e)

1.8k Views Asked by At

Of what use is this API?

I adopted it a few months ago with the thought in mind that I could use it to trap both keyboard and mouse actions in a ComboBox to invoke business logic once the user made a selection. As a developer, I went along, using the mouse only, blissfully ignorant, thinking my solution good, until the tester got involved and actually tried to use the combo box with the keyboard. It seems that itemStateChanged() has a very counter-intuitive (to me anyway) sense of what it means to change the item state.

When used with a mouse, the user can move over the droplist, with the mouse, with the droplist highlighting the combo box entry that currently contains the mouse cursor but without generating the event. The event is generated when the user actually clicks on an item. This is very intuitive and what I would expect.

On the other hand, with the keyboard, every press of the down-arrow causes an itemStateChanged event to be generated. This is counter-intuitive and not what I want. I want to generate an event only when the user singals his choice by pressing the Enter key signalling that the choice has been made - i.e. the analogue of the mouse click, not of the mouse move. Is there a way to make the combo box behave in this way or can I not use itemStateChanged for trapping these events?

I should add, also, that the ItemEvent itself contains nothing that would help me disambiguate the situation.

Update: OK, this is the nub of the issue: How can I make Swing treat down-arrowing through the drop list of a combo box as the equivalent of moving the mouse through the elements of the drop list? Both ItemListener and ActionListener consider the arrow-key action a "selection" rather than a navigation. Whereas I want the keyboard to be analogous to the mouse. Our requirement is to make everything work with the keyboard. The mouse is a nice-to-have that experienced users of this application won't use much.

2

There are 2 best solutions below

3
On BEST ANSWER

As per the Javadoc itemStateChanged(ItemEvent e) is invoked when an item has been selected or deselected by the user. And what you see is exactly the same behavior. With your arrow keys, for each key pressed you're effectively selecting (and also deselecting at the same time) one particular item from the combo.

For your situation, I think you should consider implementing your business logic as part of a JButton that servers as a confirmation of user selection or try out having a FocusListener on the JComboBox and having your logic as part of public void focusLost(FocusEvent e). Here's a tutorial to help you get a better picture for FocusListener

1
On

From the JComboBox docs

aListener will receive one or two ItemEvents when the selected item changes

Now, I'll be the first to admint that the wording is a little light, but what this is saying is, when ever a user selects and Item (from the drop down), you will be notified at least once, probably twice.

What you really want to use is a ActionListener

The ActionListener will receive an ActionEvent when a selection has been made. If the combo box is editable, then an ActionEvent will be fired when editing has stopped.