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();

}

}

[2531 byte] By [netzrota] at [2007-9-28 12:07:34]
# 1

The list exists within two different threads - the thread which and changes the list contents, and the thread which is painting the list on screen. Somewhere in the process, the two threads have different ideas about the number of elements in the list, and so an ArrayIndexOutOfBoundsException is thrown.

To solve this, you might like to try setting the list model to a brand new model with the new data already in it, rather than adding the new elements item by item.

Example:

public void run() {

startButton.setEnabled(false);

Vector v=new Vector();

for (int j = 0; j < 10; j++) {

for (int i = 0; i < 1000; i++) {

v.add(i + " - " + j);

}

}

dataList.setListData(v);

startButton.setEnabled(true);

}

I tried this about 30 times and no exceptions were thrown. Hope this helps.

Mr_Sillya at 2007-7-12 3:06:21 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...