given the following code:
public class MainFrame extends JFrame{
public MainFrame() throws HeadlessException {
super();
this.setSize(500, 400);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JButton myButton = new JButton("Test");
this.add(myButton);
this.pack();
}
public static void main(String[] args) {
new MainFrame();
}
}
Does the code inside the constructor run on the EDT. I think it does because it’s executed “inside” an instance of a JFrame, but I need a second opinion.
Continuing the idea, If I were to create other controls, for example in the main() function, that code wouldn’t be on the EDT?
Thank you!
No. You are calling the constructor from the
mainmethod which runs on the main thread.Add the usual boilerplate:
Also it’s generally a bad idea to extend classes that you don’t need to (including
JFrame,JPanelandThread). There is no need to declareHeadlessExceptionas it is unchecked.