public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Mainwindow frame = new MainWindow();
frame.setVisible(true);
}
public class MainWindow extends JFrame {
public MainWindow(){
// methods to set title, bounds, JPanels, etc.
}
public static void methodOne(){
// updates GUI
}
public static void methodTwo(){
// also updates GUI
}
Question 1: Say I call
MainWindow.methodOne
from inside static main, or from another class instantiated in static main.
is methodOne() run from EDT or Initial Thread?
Qustion 2: referring to the code at the top, inside the method
@Override
public void run(){
}
is it best practice to use:
MainWindow frame = new MainWindow();
frame.setVisibility(true);
or simply:
new MainWindow();
and have
setVisibility(true)
inside the MainWindow constructor, considering that all the methods inside MainWindow are static?
please dumb all answers down, i'm very new to coding....
Thanks in advance
Querstion 1
The code in the methods will run on the thread they are called from, doesn't matter on which thread the object that you're calling the method of was created on. When you create an object on a specific thread, only its constructor is guaranteed to run on that thread.
Also, in this case, since you have static
methods, those methods don't belong to the frame
instance, they belong to the MainWindow
class.
Question 2
What method you use depends on what you're trying to do. The first approach gives you more control, using that, you could create a new instance of the class at some point and show it later.
The second approach is usually used for the main frame, the frame that you show when the program starts you can put that statement in the class' constructor.