Re: How to get and use SwingWorker in Java 1.4?
I have some fuzzy ideas that I will attempt to explain.
It depends upon the meaning of "GUI event", and it depends upon what events
you are listening for. There are high level events (e.g. a button is
pushed, a scroll bar is scrolled), and lower level events (e.g. a window
gained focus).
One example of what can happen:
You may be listening for text entry or button pushes, but what about your
window being maximized? This will trigger a call to update(),
paintComponents() (and maybe calls to invalidate(), validate(), to the
LayoutManager, etc.), all on the EDT.
I agree completely with this possible scenario, so there is no way to
conform with the single-thread rule for a time-consuming job without
delegating it from EDT to another thread (can be the main-thread too).
The problem remains, that - without SwingWorker & Co - I have to wrap
all the Swing calls into a class in order use them from outside EDT
with invokeLater/invokeAndWait. It would be nice the have some
framework like
public class _JTextField extends JTextField implements Runnable
{
private String text;
public _JTextField(String text, int columns)
{
super(text, columns);
}
public void setText(String text)
{
this.text = text;
try
{
SwingUtilities.invokeAndWait(this);
}
catch (Exception ex) {}
}
public void run()
{
assert SwingUtilities.isEventDispatchThread() : "Not in EDT";
super.setText(text);
}
}
With it, I'am back to the simple call from any thread (other than EDT):
_JTextField tf = new _JTextField("init", 10);
...
tf.setText("run") ...