EDT and SwingUtilities.invokeLater help

P

pek

OK, I got really confused about when to use SwingUtilities or not.
For example.

Let's say that I have a simple class that extends JFrame, a private
JButton field testButton and a private JLabel field testLabel.

Somewhere down the code I right the following:

testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
testLabel.setText("testButton");
}
});

Does this needs to be converted to :

public void actionPerformed(ActionEvent e){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
testLabel.setText("testButton");
}
})
});

or not? And why..?
When exactly do I need to use SwingUtilities?
If I have a thread that changes a JLabel inside/outside the class, do
I need it?
If the thread instead of changing the testLabel invokes
testButton.doClick(), will I still need to use SwingUtilities..?

Overall, is there any good reference in the internet to explain all
these..?

Thank you very very very very much for your help..
 
N

Nigel Wade

pek said:
OK, I got really confused about when to use SwingUtilities or not.
For example.

Let's say that I have a simple class that extends JFrame, a private
JButton field testButton and a private JLabel field testLabel.

Somewhere down the code I right the following:

testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
testLabel.setText("testButton");
}
});

Does this needs to be converted to :

public void actionPerformed(ActionEvent e){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
testLabel.setText("testButton");
}
})
});

or not? And why..?

No. Event handlers (when invoked in response to actions in the GUI) are run by
the EDT. If you invoke the method manually from another thread then *that*
invocation should be wrapped in invokeLater().
When exactly do I need to use SwingUtilities?

Whenever you attempt to modify (and in most cases, read) any property of any
JComponent from a thread other than the EDT. Event handlers are a special case
because events are usually generated by user interaction with the GUI. The
event is handled by the EDT and the event handler which is invoked in response
to the event is run on the EDT.
If I have a thread that changes a JLabel inside/outside the class, do
I need it?

If the thread is not the EDT then, yes, you must use
SwingUtilites.invokeLater().
If the thread instead of changing the testLabel invokes
testButton.doClick(), will I still need to use SwingUtilities..?

I believe so, from my cursory glance at the doClick() method I see nothing which
would indicate that this method ensures that it is safe to call from a non-EDT
thread.
Overall, is there any good reference in the internet to explain all
these..?

The best starting place (at least in my opinion) is the Java Swing tutorial at
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html

and the concurrency section of the Essential Classes tutorial:
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
 
P

pek

No. Event handlers (when invoked in response to actions in the GUI) are run by
the EDT. If you invoke the method manually from another thread then *that*
invocation should be wrapped in invokeLater().


Whenever you attempt to modify (and in most cases, read) any property of any
JComponent from a thread other than the EDT. Event handlers are a special case
because events are usually generated by user interaction with the GUI. The
event is handled by the EDT and the event handler which is invoked in response
to the event is run on the EDT.


If the thread is not the EDT then, yes, you must use
SwingUtilites.invokeLater().


I believe so, from my cursory glance at the doClick() method I see nothing which
would indicate that this method ensures that it is safe to call from a non-EDT
thread.




The best starting place (at least in my opinion) is the Java Swing tutorial athttp://java.sun.com/docs/books/tutorial/uiswing/TOC.html

and the concurrency section of the Essential Classes tutorial:http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : (e-mail address removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555

Thank you all for your help. I was planning to buy Java Concurrency in
Practice for quite some time and now I just did. The book is
excellent. I recommended for more than just Swing's EDT etc.
 
P

pek

The only time you need to use in is when you are on some other thread.

Seehttp://mindprod.com/jgloss/swingthreads.html

By the way, now that we are on this subject.. Is this a good way to
write an updater thread:

Executors.newScheduledThreadPool(1).schedule(new Runnable() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
testLabel.setText("Test");
}
});
}
},1,TimeUnit.SECONDS);
}

?? It looks very ugly.. :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top