Item Events


Introduction.  We are about to study more GUI controls such as
  • check boxes - JCheckBox
  • radio buttons - JRadioButton
  • combo boxes - JComboBox

When we work with things like check boxes, radio buttons and combo boxes we need to be able to code so that the program will be able to respond to user inputs.  This is most typically done with item events

In order to make use of item events a class must implement the ItemListener interface.  This is somewhat analogous to implementing the ActionListener we have worked with in earlier sections.

There  is only one method within this interface that will be invoked by a programmer to respond to when a user interacts with a control that implements an ItemListener.  The method is outlined in the following table.


Method Description
void itemStateChanged(ItemEvent evt) This is the method that must be developed by the programmer to respond to user actions with controls that have ItemListeners.


To determine which item/control was actually used by the user there are two main built-in methods that the programmer might use.


Method Description
getItem( ) This is used to determine in which item the event occurred.
getStateChange( ) This will return an integer that is equivalent to either
  • ItemEvent.SELECTED
  • ItemEvent.DESELECTED

in order to determine what item was actually selected or deselected.