JDK-4067113 : ItemListener not fired when using methods to select/deselect
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1997-07-24
  • Updated: 1997-12-05
  • Resolved: 1997-12-05
Related Reports
Duplicate :  
Description
The ItemListener method itemStateChanged is not invoked when the state is changed via method calls.  The listener is only notified when the user changes state via user gestures.  The enclosed code will print out events when list items are selected by the mouse, but not when the "Next" button changes which item is selected.

import java.awt.*;
import java.awt.event.*;
 
public class t extends java.applet.Applet
    implements ItemListener, ActionListener
{
    private List list;
    private Button next = new Button("Next");
 
    public void init() {
        list = new List();
        for (int i = 0; i < 3; i++)
            list.add(Integer.toString(i));
 
        list.addItemListener(this);
        next.addActionListener(this);
 
        add(list);
        add(next);
    }
 
    public void itemStateChanged(ItemEvent evt) {
        System.out.println("evt = " + evt);
    }
 
    public void actionPerformed(ActionEvent e) {
System.out.println("next");
        int i = list.getSelectedIndex();
        ++i;
        i %= list.getItemCount();
        list.select(i);
    }
}

Comments
WORK AROUND The workarounds are (a) accompany every call that changes the selection state with a call to the same code that the listener would invoke, or (b) subclass List with something that overrides all the selection-modifying methods to invoke the listern callback.
11-06-2004

PUBLIC COMMENTS The ItemListener method itemStateChanged is not invoked when the state is changed via method calls. The listener is only notified when the user changes state via user gestures. The enclosed code will print out events when list items are selected by the mouse, but not when the "Next" button changes which item is selected. import java.awt.*; import java.awt.event.*; public class t extends java.applet.Applet implements ItemListener, ActionListener { private List list; private Button next = new Button("Next"); public void init() { list = new List(); for (int i = 0; i < 3; i++) list.add(Integer.toString(i)); list.addItemListener(this); next.addActionListener(this); add(list); add(next); } public void itemStateChanged(ItemEvent evt) { System.out.println("evt = " + evt); } public void actionPerformed(ActionEvent e) { System.out.println("next"); int i = list.getSelectedIndex(); ++i; i %= list.getItemCount(); list.select(i); } }
10-06-2004

EVALUATION mike.somlo@eng 1997-12-04: This seems to be a duplicate of 4034837 since both are looking for the item notification when using certain methods.
04-12-1997