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

Keyboard selection in JList generates bad ListSelectionEvents

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.0.2, 1.1.5, 1.2.0
    • client-libs
    • None
    • 1.2fcs
    • generic, unknown, x86
    • generic, solaris_2.5.1, windows_nt
    • Not verified

      Create a JList with some items in it and add a ListSelectionListener to it. Click on one of the items - notice that your ListSelectionListener is called, multiple times, with the last call having firstIndex==lastIndex, and isAdjusting==false. Now use the arrow keys to arrow up and down in the List, selecting new items. Notice that your ListSelectionListener is called only once for each arrow keypress, and firstIndex!=lastIndex (though isAdjusting==false). firstIndex differs by 1 from lastIndex. This incorrectly implies that the selection contains two list items, right next to eachother, when in fact it doesn't. It only contains one item, whose index is lastIndex.
      ====================

      Here's some code that demonstrates the issue:
      tom.santos@eng 1998-05-28

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

      public class ListTest {
      public static void main( String[] argv ) {
      JFrame frame = new JFrame( "List Test" );
      frame.addWindowListener( new WindowAdapter(){
      public void windowClosing( WindowEvent e ){
      System.exit( 0 );}});

      JList list = new JList( new Object[] { "Orion", "Coma Berenices", "Lyra", "Leo", "Triangulum" } );

      list.addListSelectionListener( new ListSelectionListener() {
      public void valueChanged( ListSelectionEvent e ) {
      System.out.println( e );
      }
      });

      frame.getContentPane().add( list );

      frame.pack();
      frame.show();
      }
      }

            erapinsunw Eric Rapin (Inactive)
            pkorn Peter Korn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: