jlist exception in thread
The application below throws exceptions occasionally and I don't know why. It's an ArrayIndexOutOfBoundsException from the JList which I can't catch. Without the thread there is no exception. Is this a problem of Java ?
import javax.swing.UIManager;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
// This application throws exceptions occasionally
// if it doesn't throw exceptions then restart the application
public class MainFrame extends JFrame implements ActionListener,Runnable{
protected JButton startButton;
protected JListdataList;
protected DefaultListModel model;
public MainFrame() {
super();
getContentPane().setLayout(new FlowLayout());
setSize(new Dimension(400,200));
setTitle("This application throws exceptions occasionally");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; }
if (frameSize.width > screenSize.width){ frameSize.width = screenSize.width; }
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
startButton = new JButton("Start");
startButton.addActionListener(this);
model = new DefaultListModel();
//model = new Vector();
dataList = new JList(model);
JScrollPane scrollPane = new JScrollPane(dataList);
scrollPane.setSize(100,50);
getContentPane().add(startButton);
getContentPane().add(scrollPane);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==startButton)
{
Thread t1 = new Thread(this);
t1.start();
}
}
public void run()
{
startButton.setEnabled(false);
for (int j=0;j<10;j++)
{
model.removeAllElements();
for (int i=0;i<1000;i++) model.addElement(i+" - "+j);
}
startButton.setEnabled(true);
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch(Exception e) { e.printStackTrace(); }
new MainFrame();
}
}

