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)
======================================================================