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

JList in single selection mode shows unexpected behavior on CTRL-A

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • generic
    • generic



      Name: yyT116575 Date: 10/20/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
      Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)


      Below is sample code to reproduce the problem.

      1. Compile and run ListQuirk
      2. Select an item in the JList to give it the focus
      3. Hit CTRL-A - all items are selected as expected
      4. Click "Single" to change mode to ListSelectionModel.SINGLE_SELECTION
      5. Select an item in the JList to give it the focus.
      6. Hit CTRL-A - the last item in the list is selected! You can only
         notice this by scrolling down to the bottom, which makes it even more
         unexpected.

      I know this isn't an earth-shaker, especially since the workaround is simple,
      but I think it's worth putting on record.

      /* Start ListQuirk.java */
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;

      public class ListQuirk
      {
          public static void main (String[] args)
          {
              ListQuirk lq = new ListQuirk ();
              lq.show ();
          }

          private JFrame _frame;
          private JList _list;
          private JToggleButton _multiple;
          private JToggleButton _single;

          private ListQuirk ()
          {
              createComponents ();
              setupLayout ();
              hookupEvents ();
          }

          private void show ()
          {
              _frame.pack ();
              _frame.setLocation (100, 100);
              _frame.setVisible (true);
          }

          private void createComponents ()
          {
              _frame = new JFrame ("List quirk");
              _list = new JList (makeTestData ());
              _multiple = new JToggleButton ("Multiple");
              _single = new JToggleButton ("Single");

              ButtonGroup group = new ButtonGroup ();
              group.add (_multiple);
              group.add (_single);
              _multiple.setSelected (true);
          }

          private void setupLayout ()
          {
              Container c = _frame.getContentPane ();
              c.add (BorderLayout.CENTER, new JScrollPane (_list));
              JPanel buttons = new JPanel (new GridLayout (1, 2, 3, 3));
              buttons.add (_multiple);
              buttons.add (_single);
              c.add (BorderLayout.SOUTH, buttons);
          }

          private void hookupEvents ()
          {
              ActionListener modeSetter = new ActionListener ()
              {
                  public void actionPerformed (ActionEvent event)
                  {
                      int mode = event.getSource () == _multiple ?
                          ListSelectionModel.MULTIPLE_INTERVAL_SELECTION :
                          ListSelectionModel.SINGLE_SELECTION;
                      _list.clearSelection ();
                      _list.getSelectionModel ().setSelectionMode (mode);
                  }
              };

              _multiple.addActionListener (modeSetter);
              _single.addActionListener (modeSetter);

              _frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          }

          private String[] makeTestData ()
          {
              String[] testData = new String [100];
              Random random = new Random ();
              for (int i = 0; i < testData.length; i++)
              {
                  testData[i] = Integer.toString (random.nextInt ());
              }
              return testData;
          }
      }

      /* End ListQuirk.java */
      (Review ID: 110390)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: