I have a 'clear' JButton that when pressed clears a JList selection by
calling aListSelectionModel.clearSelection(). This is Fine.
However, it would seem that clearing the selection changes the list
visually but the model does not 'forget' the selection. Basically,
using the down arrow on a cleared JList seems to set the selected
index to the last selected index + 1. (Using the down arrow a list for
the first time - when nothing has been selected yet - selects item at
index 0!)
The (rough) code below illustrates this odd behavior. Select an item
from the list; press the clear button; tab back to the list. Am I not
clearing the selection the correct way? Comments?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class ListTest extends JFrame {
public ListTest() {
super();
JButton clearButton = new JButton("Clear Selection from List");
String[] data = {"one", "two", "three", "four", "five",
"six","seven","eight","nine","ten"};
final JList aList = new JList(data);
JScrollPane sp = new JScrollPane(aList);
getContentPane().add(clearButton, BorderLayout.CENTER);
getContentPane().add(sp,BorderLayout.EAST);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}}
);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
aList.getSelectionModel().clearSelection();
}
}
);
setSize(400,200);
}
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.setVisible(true);
}
}
Kleopatra - 29 Sep 2003 08:41 GMT
> However, it would seem that clearing the selection changes the list
> visually but the model does not 'forget' the selection. Basically,
> using the down arrow on a cleared JList seems to set the selected
> index to the last selected index + 1. (Using the down arrow a list for
> the first time - when nothing has been selected yet - selects item at
> index 0!)
navigation in the JList is based on the leadIndex of the selectionModel.
DefaultListSelectionModel does not update the lead properly on clearing
the
selection. It's an old issue showing in several bug reports (see f.i.
#4334792, #4338140, #4303294) - and marked as fixed for Tiger in some of
them.
Greetings
Jeanette
Rick - 29 Sep 2003 20:41 GMT
I was not aware that this was an existing problem.
Thanks for referring me to the bug report. I will look more carefully
there in the future!
Rick