Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2337

Re: Need simple example of how to safely pass data from worker to EDT swing threads.

Newsgroups comp.lang.java.help
Date 2012-12-06 09:09 -0800
References <118db2e0-86dc-4d6b-aabb-ba68ee8c2f53@googlegroups.com> <k9qdk0$790$1@dont-email.me>
Message-ID <f47dd7a1-53b2-465e-a082-49adc790d3e6@googlegroups.com> (permalink)
Subject Re: Need simple example of how to safely pass data from worker to EDT swing threads.
From kedward777@gmail.com

Show all headers | View raw


> 
>      When the worker has a result (or intermediate result) and wants
> 
> to modify the GUI, one approach is to create a Runnable object that
> 
> knows the result and calls methods of JComboBox or whatever.  The
> 
> worker then uses the invokeLater() or invokeAndWait() methods
> 
> of the SwingUtilities class to tell Swing to execute the Runnable
> 
> on the EDT.  When the Runnable actually runs, it's on the EDT and
> 
> can safely manipulate the GUI.  Here's an example using a JLabel:
> 
> 
> 
> 	// Instance variable somewhere in the GUI:
> 
> 	JLabel label;
> 
> 
> 
> 	// On the EDT while creating the GUI:
> 
> 	label = new JLabel("Watch this space");
> 
> 	someContainer.add(label);
> 
> 	...
> 
> 
> 
> 	// On a worker thread:
> 
> 	final String text = slowAndComplicatedComputation();
> 
> 	SwingUtilities.invokeLater(new Runnable() {
> 
> 	    @Override
> 
> 	    public void run() {
> 
> 	        label.setText(text);
> 
> 	    }
> 
> 	});
> 
> 
> 
>      It would not be safe to call label.setText() directly from
> 
> the worker thread, but you're not doing that: Swing will execute
> 
> the Runnable on the EDT, where label.setText() can operate happily.
> 
> 
> 
>      The on-line Tutorial has a readable explanation of this and of
> 
> other ways to coordinate the activities of worker threads.  See
> 
> <http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html>
> 

Thank you Eric! It worked perfectly. I am learning lots. Thanks for your patience!

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need simple example of how to safely pass data from worker to EDT swing threads. kwiateks@gmail.com - 2012-12-06 07:07 -0800
  Re: Need simple example of how to safely pass data from worker to EDT swing threads. Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-06 10:29 -0500
    Re: Need simple example of how to safely pass data from worker to EDT swing threads. kedward777@gmail.com - 2012-12-06 09:09 -0800
  Re: Need simple example of how to safely pass data from worker to EDT   swing threads. Nigel Wade <nmw@ion.le.ac.uk> - 2012-12-06 17:27 +0000
    Re: Need simple example of how to safely pass data from worker to EDT   swing threads. Lew <lewbloch@gmail.com> - 2012-12-06 11:10 -0800
  Re: Need simple example of how to safely pass data from worker to EDT swing threads. Roedy Green <see_website@mindprod.com.invalid> - 2012-12-06 15:03 -0800

csiph-web