Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Updating Swing Components From Other Threads (Is this how?)
I've been stuck on this for a while (and several posts).  It's part of
something bigger, so it's kind of hard to post code here.  What I'd like to
know is if this outlined example (below) is a safe way to update Swing
components from other threads.  When the user clicks on a JButton, my goal
is to disable several buttons, open a new "Waiting..." window, run an
external program, wait until the external program is done, then close the
"Waiting..." window and enable the buttons.  Is this how (in general) to do
it:

1. The actionPerformed listener creates a thread to run the external
program
2. After creating the new thread, I can disable buttons and open a window in
the actionPerformed listener.
3. The new thread includes a (not necessarily) anonymous Runnable object
that closes the new window and enables the disabled buttons.
4. I use EventQueue.invokeLater(Runnable) to run the Runnable object created
in #3 above, then run the external program.

If I understand this correctly, creating a thread in the ActionListener is
necessary so the listener can update the display while the thread is
running.  Inside this new thread I can't access a Swing object directly
because Swing objects aren't thread safe.  I can, however, update a Swing
component from a new thread by creating a Runnable object, using
EventQueue.invokeLater() to run it, and access the Swing object ONLY from
the Runnable that is invoked later, since that will turn the updating over
to the event dispatching thread and this way the only thread doing any
updating of Swing objects is the EDT.

Is this right?  This has been a hard topic for me to grasp, so I hope this
works and I finally have a safe way to do this.

Thanks!

Hal

Report this thread to moderator Post Follow-up to this message
Old Post
Hal Vaughan
10-27-04 08:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:42 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.