Frank Frank - 9 months ago 39
Java Question

Java Swing BasicUI update error, what can I do?

My program uses Swing JPanel, JList, JScrollPane ...

It runs fine, but generated the following error message, and yet in the message it didn't say which line of my program caused the error, what can I do ?


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3

at javax.swing.plaf.basic.BasicListUI.updateLayoutState(
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(
at javax.swing.JComponent.getPreferredSize(
at javax.swing.ScrollPaneLayout.layoutContainer(
at java.awt.Container.layout(
at java.awt.Container.doLayout(
at java.awt.Container.validateTree(
at java.awt.Container.validate(
at javax.swing.RepaintManager.validateInvalidComponents(
at javax.swing.SystemEventQueueUtilities$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(


I do have various .validate() and .repaint() statements in my program to make sure it runs correctly. Because my program looks fine, does that mean I can ignore the error ? Anything I can do to avoid the error message ?

Here is more details:

<1> Java version jdk1.6.0_11

<2> How I init the list:

for (int Selector_Id=0;Selector_Id<6;Selector_Id++)
Stock_Symbol_Data[Selector_Id]=new DefaultListModel();
Stock_Symbol_List[Selector_Id]=new JList(Stock_Symbol_Data[Selector_Id]);
Stock_Symbol_ScrollPane[Selector_Id]=new JScrollPane(Stock_Symbol_List[Selector_Id]);

if (Selected_Symbols_Vector.size()>0)
for (int i=0;i<Selected_Symbols_Vector.size();i++)

Yishai is right, since my program needs to init a very long list, which requires about a minute. I can't wait to see the UI before the init is finished, so I put it in a "SwingWorker" class and let it do the init after the app UI window opens; that way I can see the progress from within the UI rather then wait for the first window to open. It seems to me it's the PC's slowness that's messing up the UI update process; if I later on move to a faster machine, Swing should straighten this out, or am I right about this?

I tried to use the "(wrap the change in a Runnable and call SwingUtilities.invokeLater)" approach, but it didn't work as I expected. It waits till all the list is filled in, then opens the first window; that means I have to look at empty screen for one minute before the first UI shows up.

With SwingWorker, it now shows the error message randomly--sometimes here, sometimes there, other times not at all.

My SwingWorker looks like this :

class Update_Selection_Worker extends SwingWorker<Integer,Integer> // Look into SwingWorkerDemo in Fit for details
int Selector_Id;
boolean Update_Only_This_Selector;
Stock_Image_Scanner Stock_image_scanner;

public Update_Selection_Worker(int Selector_Id,boolean Update_Only_This_Selector,Stock_Image_Scanner Stock_image_scanner)

protected Integer doInBackground() throws Exception
// Out(" In Update_Selection_Worker Selector_Id="+Selector_Id);

if (Update_Only_This_Selector) // Only need to update from Rules_Panel_Id, eariler ones haven't changed
else for (int i=Selector_Id;i<Stock_image_scanner.Rules_Panel_Count;i++)

return 1;

protected void process(java.util.List<Integer> chunks) // As the worker thread executes, it can publish results of V type. Override the process method to work with intermediate results.
for (final int i : chunks)
SwingUtilities.invokeLater(new Runnable()
public void run()



protected void done()

public static void out(String message) { System.out.print(message); }
public static void Out(String message) { System.out.println(message); }

Answer Source

I guess too the exception is because the concurrent modification of some swing object array.

SwingWorker has the option for 'in progress events'. You need to override process(List<V> chunks) protected method and use the void publish(V... chunks) method to send status update to the UI. In your processing case this means submit your partial results periodically so the user doesn't get bored.