I have a JList component which should be emptied and repopulated. The following code (based on my original code) shows a simple window with a JList and a JButton:
import java.awt.BorderLayout;
import javax.swing.*;
public class JListTest extends javax.swing.JFrame{
JList jList;
JButton button;
DefaultListModel model;
public JListTest() {
jList = new JList();
model = new DefaultListModel();
jList.setModel( model );
button = new JButton();
getContentPane().add(jList, java.awt.BorderLayout.CENTER);
button.setText("add 10000 items");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
model.clear();
for( int i=0; i<10000; ++i ) {
model.addElement( "aaaa");
}
}
});
getContentPane().add(button, BorderLayout.PAGE_START);
pack();
}
public static void main(String args[]) {
JListTest jlt =new JListTest();
jlt.setSize(300, 300);
jlt.setVisible( true );
}
}
If I press the button the insertion (10000 items) is very fast. If I press it again and again it is still very fast.
If I select the third item and press the button, the result is the same, the insertion is very fast.
If I select the first item and press the button, the program becomes very slow (actually I have to stop it).
Why the selection of the first item slows down the execution?
I've tested it using JDK 1.5 and 1.6.