Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GUI update and threads

0 views
Skip to first unread message

Karsten Wutzke

unread,
Mar 30, 2004, 5:03:34 PM3/30/04
to
Hello all!

I use Observer-Observable to update my GUI when desired (setChanged,
notifyObservers). However, I sometimes get an exception irregularly:

java.lang.ArrayIndexOutOfBoundsException: No such child: 4
at java.awt.Container.getComponent(Container.java:237)
at javax.swing.JComponent.rectangleIsObscured(JComponent.java:3705)
at javax.swing.JComponent.paint(JComponent.java:806)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4795)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4748)
at javax.swing.JComponent._paintImmediately(JComponent.java:4692)
at javax.swing.JComponent.paintImmediately(JComponent.java:4495)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


I have three threads running which call methods on objects that lead to
an update of the GUI, so I assume it's a threading problem (due to the
irregularities).

I remember from some time ago, that there was a way to have the AWT
thread handle the repaint (or was it the event dispatcher? or are they
the same?).

Does anyone know how I can post the GUI updates to the "Java GUI repaint
thread"? I can't remember how it was done, I think it was a simple
static method call on some class, but which one?

Can anyone make some other suggestions as to what I could try?

Thanks very much!
Karsten

A. Bolmarcich

unread,
Mar 30, 2004, 6:08:15 PM3/30/04
to
In article <c4cqrn$lt0$00$1...@news.t-online.com>, Karsten Wutzke wrote:
> I use Observer-Observable to update my GUI when desired (setChanged,
> notifyObservers). However, I sometimes get an exception irregularly:

[snipped exception tracreback]

> I have three threads running which call methods on objects that lead to
> an update of the GUI, so I assume it's a threading problem (due to the
> irregularities).
>
> I remember from some time ago, that there was a way to have the AWT
> thread handle the repaint (or was it the event dispatcher? or are they
> the same?).
>
> Does anyone know how I can post the GUI updates to the "Java GUI repaint
> thread"? I can't remember how it was done, I think it was a simple
> static method call on some class, but which one?

Because you are using Swing (the snipped exception traceback included
javax.swing classes), you should use the SwingUtilities.invokeLater(
Runnable) method. Here is an example from the Java documentation

Runnable updateAComponent = new Runnable() {
public void run() { component.doSomething(); }
};
SwingUtilities.invokeLater(updateAComponent);

The run() method of the updateAComponent Runnable will be invoked by
the event dispatch thread.

Olivier Chafik

unread,
Mar 31, 2004, 4:11:01 AM3/31/04
to
javax.swing.SwingUtilities.invokeLater(Runnable)
javax.swing.SwingUtilities.invokeAndWait(Runnable) throws ...

Karsten Wutzke wrote:

--
挨oOoぐlivier

0 new messages