Hi everybody, I'm Luca.
I have a doubt about the java event model (jdk1.4.2):
In Motif all modifications to the widgets properties have to be made
from inside the event loop through some callback; if an external task
has to modify - for example - the background color of a text field,
then you have to register a new input source for x in the form of a
writing or reading to or form a file descriptor...
In java there seems not to be this restriction, I mean, if, for example
I have a thread that has to change the text of a text area, it can call
the JTextArea setText method on the interested text area component and
the text will be updated, while doing a similar thing in X/Motif could
cause an async reply from the X server...
Am I right?
Thank you
Luca
Jan van Mansum - 06 Oct 2005 16:41 GMT
Yes, that's right. However, you can still make sure that GUI-related
tasks are executed synchronously by putting them on the
EventDispatchThread (EDT) queue, e.g.,
public void setTextSynchronously(final JTextArea myTextArea)
{
EventQueue.invokeAndWait(new Runnable() {
public void run()
{
myTextArea.setText("blah");
}
});
}
Vova Reznik - 06 Oct 2005 17:07 GMT
> Yes, that's right. However, you can still make sure that GUI-related
> tasks are executed synchronously by putting them on the
[quoted text clipped - 9 lines]
> });
> }
Not really.
It will not executed synchronously.
It will put execution at the end of queue.
Also this example will not compile because it doesn't handle
InterruptedException and InvocationTargetException.
And it will keep your thread because of using invokeAndWait
(Wait for finishing)
And using invokeAndWait doesn't recommended by Sun.
Use invokeLater instead.
Jan van Mansum - 07 Oct 2005 11:11 GMT
I think that is what you call "synchronous execution": the calling
thread waits for the call to finish before proceeding. That is exactly
what invokeAndWait does. If the code after setText depends on the text
being set correctly that is what you should do. Otherwise you may
indeed use invokeLater.
Roedy Green - 06 Oct 2005 20:10 GMT
>I have a thread that has to change the text of a text area, it can call
>the JTextArea setText method on the interested text area component and
>the text will be updated, while doing a similar thing in X/Motif could
>cause an async reply from the X server...
All swing methods must be called from the Swing event thread. See
http://mindprod.com/jgloss/swingthreads.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.