I am very curious why we have to use java.awt.EventQueue.invokeLater
to control swing components.
Why can't we do that in a normal thread? What exactly is going on behind the scenes? From what I have noticed if I have a JFrame
I can set visibility to true or false from the main thread without getting any errors, and it does seem to work. So what exactly do I achieve by using java.awt.EventQueue.invokeLater
? I am also fully aware that I can use SwingUtilities.invokeLater
but as explained here, they seem to be one and the same thing.
Thanks to anyone for their explanation. Hopefully this is a valid question.
EDIT: to answer wumpz question We can create a jframe
JFrame frame = new JFrame("Hello world");
frame.setSize(new Dimension(300, 300));
frame.setPreferredSize(new Dimension(300, 300));
frame.setMaximumSize(new Dimension(300, 300));
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
And on the same thread it was created do the following.
for (int i = 0; i < 34; i++)
{
System.out.println("Main thread setting to "+(!frame.isVisible()));
frame.setVisible(!frame.isVisible());
}
And no complaints.