Re: Need simple example of how to safely pass data from worker to
EDT swing threads.
On 12/6/2012 10:07 AM, kwiateks@gmail.com wrote:
Hello Kinds Sirs,
I need a simple runnable example of a swing application that has a GUI with a jcombobox filled with a long list of name/value objects (eg 3000 employee names and numbers). There is also a worker process that reads in a record from a file/database, and then updates the gui jcombo box so that the employee item is "selected".
I was able to write something using java map and employee objects to set the selected item in the jcombo box, but if I invoke setSelectedItem in the worker thread, I get odd results/delays.
See below snippets... how should the worker thread pass the employee object back to the gui for it to setSelectedItem?
The snippet seems uninformative, so I'll ignore it and just
tackle the question. All (or nearly all) manipulation of Swing
components must occur on the Event Dispatch Thread, so you can't
just have the worker thread call the methods of JComboBox.
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>
--
Eric Sosman
esosman@comcast-dot-net.invalid