Swing Action & Event Dispatch Thread
wrt the Swing Action Architecture. Is it thread safe or do I need to make
a point of executing commands such as [Action].setEnabled() on the EvtDispatch Thread? - ie. with invokeLater or invokeAndWait.
I'm aware that building, modifying and querying Swing components
must be done on the EDT. But as Actions aren't 'Components', but are
part of the Swing Architecture, I'm hoping they have internally implemented the calls to EDT.
Thanks,
[479 byte] By [
Maxiia] at [2007-11-27 6:52:24]

# 1
Not sure exactly what you mean, but if you mean what I think you mean ...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MakeAFrame {
private JFrame frame;
private JPanel panel;
private JButton button;
private JLabel label;
public MakeAFrame(String name) {
frame = new JFrame(name);
panel = new JPanel();
button = new JButton("GO!");
label = new JLabel();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
label.setText(""+Math.random()*100);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
panel.setLayout(new GridLayout(2,0,20,20));
panel.add(button);
panel.add(label);
c.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] argv) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MakeAFrame("HI!");
}
});
}
}
... Then, yes, you can do that.
# 2
Well typically and Action is executed as part of an ActionListener and ActionListeners (like other listeners) execute in the EDT, so no you don't have to worry about the invokeLater().
But if the setEnabled() is executed outside the EDT then yes you need to worry. So you need to know the design of your system.
# 3
Thanks for both replys. I found them both helpful.
camickr, you are correct. The issue is I'm calling the [Action].setEnabled
from outside the EDT. In addition to the AWT Evt Mgmt System, I've also
got my own GameEvent Mgmt system. It is in response to these events
(not executed in the EDT) that I find myself calling the Action methods.
My confusion came from the fact that Actions aren't components, but they are connected to components. I was not clear whether the Swing designers had recognized that these Actions could be used as the basis for more Program-wide commands, and therefore called outside
the EDT. I guess they just decided that it was better to get the best performance within normal EDT use, and for those of us that used the Architecture more broadly, we could just call it from the EDT.
Thanks, the discussion clarified my thinking. It makes design sense that you don't penalize performance for the normal use of the classes for more peripheral uses.
Best,
Maxiia at 2007-7-12 18:26:55 >
