Q:

How to run some code when a window (jframe) loads in java?

I have made a program in Java that has two windows (JFrames):
a) The Log-in Window,
b) The Main Window.
The "Log-in Window" calls the "Main Window" and then closes.
How can I run some code when the "Main Window" is loading (run some code only once, when the "Main Window" frame opens for the first time)?
I try the below code in the main() method of the "Main Window" but it doesn't work...
Code in Main-Window.java file:
public static void main(String[] args) {

  EventQueue.invokeLater(new Runnable() {
    public void run() {
      try {

        final Main_Window frame = new Main_Window();            
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
          public void windowOpened(WindowEvent e) {
            runSomeCode();
          }
        });

      } catch (Exception e) {
        e.printStackTrace();
      }

    }

  });

}

The runSomeCode(); never executes...
The way that the "Log-in Window" closes and calls the "Main Window" is this:
Code in Log-in-Window.java file:
Main_Window mw = new Main_Window();
mw.setLocationRelativeTo(null);
mw.setVisible(true);

WindowEvent winCloseEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winCloseEvent);

A:

This is kind of the purpose of the constructor.  Since you have created a new class, Main_Window, you should include the code you want to run during the creation of the main window there.  While your current definition of the Main_Window class is not present, it would be something like this.
public class Main_Window extends ?Frame implements ? {
    // members

    // constructor
    Main_Window(){
        //do some stuff here
    }

    // methods
}

