How I tried to find out if I can answer this question earlier today . I realized that I do not quite understand Event Dispatch Thread ( EDT ). Googling confirmed and helped with this and clarified why I didn’t . ( This may also be relevant to understanding.)
The code sets the GUI, and later (as in the previous question) updates the text field until the flag is removed.
I have a few questions / queries.
Please explain why the code below works fine if both calls (before swingInit and doIt ) are outside the invokeLater block (as shown), since both calls affect or request a GUI but are not executed on the EDT (are they?). Isn't that an attractive failure?
The code also runs if the swingInit call is inside and doIt outside invokeLater . So swingInit runs on EDT, but shouldn't doIt not run on EDT? (I was surprised that it worked. Should I be?)
I understand why it hangs if doIt is inside invokeLater , no matter where swingInit : the goal of invokeLater is ONLY to initialize the GUI (on the right?).
Should doIt triggered (possibly from an event) on the EDT, but certainly not inside the invokeLater block?
(The history of the EDT concept is interesting. This was not always the case. See the link above to “why I don't understand this.”)
import static java.awt.EventQueue.invokeLater; import java.awt.event.*; import javax.swing.*; public class Whatever { static boolean flag = true; static JTextField tf = new JTextField("Hi",20); static JPanel p = new JPanel(); static JFrame f = new JFrame(); static JButton b = new JButton("End"); public static void main(String[] args) { swingInit(); invokeLater ( new Runnable() { @Override public void run() {
java event-dispatch-thread invokelater
DSlomer64
source share