Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4129305

JList ListSelectionEvent more than once

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.0.2, 1.1.5
    • client-libs
    • generic, x86
    • solaris_2.5.1, windows_nt



      Name: rk38400 Date: 04/15/98


      JList will notify objects that hava registered as ListSelection listeners more than once for every time the list selection changes.
      If you run the sample below, you'll notice that an event fires more than once when you change the selection.
      It apprears than the event is fired once for the previous item and then again for the new current item.
      However, the behavior is not always consistent.
      Also, if you use the keyborad instead of the mouse, you'll notice that the second item on the list (index 1) is
      show as having an index of zero, so that ends up throwing all the indexes other than zero off by one.

      Sample Code:

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.event.*;

      public class ListSelectionListenerTest extends JFrame implements ListSelectionListener {
      public static void main(String[] args) {
      ListSelectionListenerTest test = new ListSelectionListenerTest();
      test.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      });
      test.pack();
      test.setVisible(true);
      }

      public ListSelectionListenerTest() {
      java.util.Vector set = new java.util.Vector();
      set.addElement("One");
      set.addElement("Two");
      set.addElement("Three");
      JList list = new JList(set);
      getContentPane().add(list);
      list.addListSelectionListener(this);
      }

      public void valueChanged(ListSelectionEvent e) {
      System.out.println("got a selection event for item " + e.getFirstIndex());
      }
      }
      (Review ID: 27903)
      ======================================================================

            erapinsunw Eric Rapin (Inactive)
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: