Search code examples
javaswingjcomboboxitemlistener

ItemListener.itemStateChanged(ItemEvent e)


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.


Solution

  • 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