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

JList: Adding Item May Extend the Current Selection

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JList will extend the selection if an item is inserted at the low end of the selection. For example, if a JList contains 5 items with item number 3 selected and new item is inserted at index 2 (item number 3's index), the selection will now be both item number 3 and the new item. The problem arises when I want to select the new item (with JList.setSelectedValue(...)), the selection will not change because the ListSelctionModel recognizes that the new item is already selected. In order to get this desire behavior. I have to:
      1. add the new item
      2. clear the selection with JList.clearSelection()
      3. then set the selection for the new item with JList.setSelectedValue(...)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JList component
      2. Select an item in the list
      3. Insert a new item at the index of the selection
      4. Use JList.setSelectedValue(...) to select the new item

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Only the new item should be selected.
      ACTUAL -
      Both the previously selected and the new item are selected.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class TestFrame extends JFrame {
         protected JList jList = new JList(new DefaultListModel());
         protected JButton addButton = new JButton();
        
         protected int itemNumber = 0;

         public TestFrame() {
            super("JList Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(100,200);
            JPanel panel = (JPanel)getContentPane();
            panel.setLayout(new BorderLayout());
            panel.add(jList, BorderLayout.CENTER);
            panel.add(addButton, BorderLayout.SOUTH);
            addButton.setText("Add Item");
            addButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                  DefaultListModel model = (DefaultListModel)jList.getModel();
                  int index = jList.getSelectedIndex();
                  String text = "Item #"+(++itemNumber);
                  model.add(index,text);
                  jList.setSelectedValue(text, false);
               }
            });

            DefaultListModel model = (DefaultListModel)jList.getModel();
            String text = "===LAST ITEM===";
            model.add(0,text);
            jList.setSelectedValue(text, false);
         }

         public static void main(String[] unused) {
            new TestFrame().setVisible(true);
         }
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Imported:
              Indexed: