Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

the java event model

Thread view: 
lcdll@libero.it - 06 Oct 2005 15:53 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.