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]
# 1
By the way, I use Java 1.5, in case anyone wonders. Is this a Java bug or am I doing omething I am not supposed to do?
shelziba at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 2
Just ran it with Java 1.4.2_07, and it worked like a charm. Anyone know what has been introduced into 1.5 when it comes to JList?-r
shelziba at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 3
Try creating a new list model. Initialising it with all your new data, and then setting the list model into the list. Could be some really inneficient code happening when adding a new element. Or at least try having a lower number of new elements, and seeing if that hangs it too.
justinlawlera at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 4
I have hacket my way around the problem, so that is OK. The wierd thing is that it hangs when run with a 1.5 JRE and not a 1.4 JRE.
shelziba at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 5

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.

KPSeala at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 6

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

shelziba at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 7

> 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.

Niceguy1a at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks!Used the FastListModel workaround and it was beautiful to watch :)-r
shelziba at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...
# 9
another possibilitypublic void actionPerformed(ActionEvent e) {if(listModel.size() > 0) list.setSelectedIndex(1);//<add this linelistModel.clear();may need a flag set to stop any listener action
Michael_Dunna at 2007-7-13 23:24:15 > top of Java-index,Desktop,Core GUI APIs...