JDK-4334792 : Lead Index in DefaultListSelectionModel will never return to -1
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0,11
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2000-05-01
  • Updated: 2018-03-08
  • Resolved: 2018-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11Resolved
Related Reports
Relates :  
Relates :  
Description
Name: skT45625			Date: 05/01/2000


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


After once selecting an element in a JList using the DefaultListSelectionModel
the variable leadIndex in the DefaultListSelectionModel will never return to -
1, which is the initial value. As a result a caret will always be painted
around one of the list elements (the one the leadIndex points at).

The following code will print out a couple of lines that should make the
problem clear. After clearing the selection or removing all elements from a
JList the lead index should be set to -1. But even after removing all elements
from a list the lead index is still >-1, even though there are no elements to
point at.

As far as I can see there is no way to set the leadIndex to -1 after once an
element was selected.

import javax.swing.*;

public class JListTest {
  JList myList = new JList();
  DefaultListModel myModel = new DefaultListModel();
  DefaultListSelectionModel mySelectionModel = new DefaultListSelectionModel();

  public JListTest() {
    myList.setModel(myModel);
    myList.setSelectionModel(mySelectionModel);

    // add elements
    myModel.addElement("Test1, Element1");
    myModel.addElement("Test1, Element2");
    myModel.addElement("Test1, Element3");
    myModel.addElement("Test1, Element4");
    System.out.println();
    System.out.println("List filled with elements");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());


    // select something
    myList.setSelectedIndex(3);
    System.out.println();
    System.out.println("Select index 3");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // unselect
    myList.setSelectedIndex(-1);
    System.out.println();
    System.out.println("Clear selection by setting selection index to -1");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // select something else
    myList.setSelectedIndex(2);
    System.out.println();
    System.out.println("Select index 2");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // unselect in a different way
    myList.clearSelection();
    System.out.println();
    System.out.println("clear selection, 2nd try");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // clear List
    myModel.removeAllElements();
    System.out.println();
    System.out.println("Remove all elements from list");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // add elements
    myModel.addElement("Test2, Element1");
    myModel.addElement("Test2, Element2");
    myModel.addElement("Test2, Element3");
    myModel.addElement("Test2, Element4");
    System.out.println();
    System.out.println("List filled again");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());

    // try to set lead index to -1
    mySelectionModel.setLeadSelectionIndex(-1);
    System.out.println();
    System.out.println("Try to set lead index to -1");
    System.out.println("Selected Index: " + myList.getSelectedIndex());
    System.out.println("Lead Index: " + mySelectionModel.getLeadSelectionIndex
());


  }

  public static void main(String[] args) {
    JListTest jListTest = new JListTest();
    jListTest.invokedStandalone = true;
  }
  private boolean invokedStandalone = false;
}
(Review ID: 104326) 
======================================================================

Comments
This bug talks about four scenarios 1) JList.setSelectionIndex(-1) The doc says JList.setSelectedIndex() should select a single cell, but it doesn't mean it should clear selection if the argument is -1. This will also not be fixed as this is not an issue and behavior is expected. 2) JList.clearSelection() The may not be fixed as developers are expected to call set Lead/Anchor after calling clearSelection. 3) ListModel.removeAllElements()/ListModel.clear This issue has been addressed already except the case when the lead/anchor is 0/0 and then JList.removeAllElements/ListModel.clear is called, then the anchor/lead is not set properly. This case is addressed in the proposed fix. This will also fix the https://bugs.openjdk.java.net/browse/JDK-6481195 4) ListSelectionModel.setLeadSelectionIndex() This is not an issue as lead can be set to -1 by setLeadSelectionIndex, if the anchor is already set properly. This is expected behavior and will not be changed. Because of all this information, this bug can be considered a duplicate/similar bug of https://bugs.openjdk.java.net/browse/JDK-6481195
08-03-2018

the problem while calling model.removeAllElements or model.clear has been resolved for all cases except when only 0 index was selected. ListSelectionModel.clearSelection problem is still present
08-02-2018

EVALUATION Low priority bug - decommitted from JDK7
26-01-2011

EVALUATION Name: apR10133 Date: 12/19/2001 1) The doc says JList.setSelectedIndex() should select a single cell and it doesn't mean it should clear selection if the argument is -1 2) JList.clearSelection() just calls the method ListSelectionModel.clearSelection() and the last one needs to be fixed (see the suggested fix). 3) JList.removeAllElements() should set the lead and anchor to -1 This problem will be fixed with the fix for 4338140. 4) ListSelectionModel.setLeadSelectionIndex() is safe from the calling with the argument "-1" and it is a quite reasonable. I guess we needs to fix the problems 2 and 3, while the 1 and 4 is not a bug. ###@###.### ======================================================================
25-09-2004

WORK AROUND Name: skT45625 Date: 05/01/2000 Create a new DefaultListSelectionModel and pass it to the JList to get set the lead index to -1. Please agree that there must be a more elegant solution ;) ======================================================================
25-09-2004

SUGGESTED FIX Name: apR10133 Date: 12/19/2001 I would suggest to change the clearSelection() method in the DefaultListSelectionModel as following: public void clearSelection() { updateLeadAnchorIndices(-1, -1); changeSelection(minIndex, maxIndex, MAX, MIN); } See the bug #4338140 for the fix to the case of removing all elements in the list. ###@###.### ======================================================================
25-09-2004