I am stumped...
Try the following code:
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
publicclass TestListextends JFrame{
private DefaultListModel listModel =new DefaultListModel();
public TestList(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list =new JList(listModel);
list.setBackground(Color.WHITE);
final JButton btnTest =new JButton("Reload");
btnTest.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
listModel.clear();
for(int i = 0; i < 4000; i++){
listModel.addElement("testing");
}
}
});
JPanel panel =new JPanel();
panel.add(btnTest);
add(new JScrollPane(list),"Center");
add(panel,"South");
setSize(400, 200);
setVisible(true);
}
publicstaticvoid main(String[] args){
new TestList();
}
}
Do the following steps:
1. Run the program
2. Press "Reload"
3. Select a random element, except the first one.
4. Press "Reload"
5. Select the first element.
6. Press "Reload"
My question is: Why does the program hang on point 6? Look at the button as you press it. It does not hang if you select any other element.
Thanks for any pointers.
Richard
[2567 byte] By [
shelziba] at [2007-10-2 20:41:02]

It seems to be associated with the lead/anchor selection indices not being reset correctly in DefaultListSelectionModel when clearing the list model.
If you clear the model they're set to 0 if you've selected the first row. They're set to -1 in all other cases. If it's 0 a value changed event is fired from the list selection model for every item that's added, otherwise no event is fired.
Extend DefaultListSelectionModel and put some logging in the fireValueChanged(int i, int j) method.
Weird.
Hope this helps.
Thanks. Ok, I reduced the number of elements to 10, and made this ListSelectionModel:
import javax.swing.DefaultListSelectionModel;
public class MyListSelectionModel extends DefaultListSelectionModel {
public MyListSelectionModel() {
super();
}
protected void fireValueChanged(int i, int j) {
System.out.println("FVC: " + i + " " + j);
super.fireValueChanged(i, j);
}
}
And set it in my code. Here are 2 test runs with results:
* Selecting item number 2 and pressing Reload:
FVC: 1 1
FVC: -1 1
* Selecting item number 1 and pressing Reload:
FVC: 0 0
FVC: 0 0
FVC: 0 1
FVC: 1 2
FVC: 2 3
FVC: 3 4
FVC: 4 5
FVC: 5 6
FVC: 6 7
FVC: 7 8
FVC: 8 9
FVC: 9 10
An event is actually fired every time. Is it supposed to be like this? It doesn't seem to happen in earlier JRE's. Is there any way to keep it from happening without ruining other functionality. I have a feeling that killing events can lead to worse behavior...
-r
> An event is actually fired every time. Is it supposed
> to be like this? It doesn't seem to happen in earlier
> JRE's. Is there any way to keep it from happening
> without ruining other functionality. I have a feeling
> that killing events can lead to worse behavior...
There are several entries in the bug parade with workarounds:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4121430
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4549174
Jim S.