0

I'm creating a standalone data management program for a competition (so I won't post all the code, only relevant stuff)--however, I am testing it in Eclipse Neon at the moment because it's not done. Also, I used WindowBuilder, if that matters (I don't think it should.)

I have one JFrame (call it "welcome") that opens when the application is first launched. In "welcome", I have a JButton that opens a different JFrame (call it "emp") and closes "welcome" (via frame.dispose().) If I exit "welcome" (with the default upper-right-corner X), then the program terminates. However, if I click on the Jbutton to open "emp" and then close "emp" in the same manner, the program does not terminate. My question is how to make the program terminate when "emp" is exited.

My code for the first JFrame ("welcome"):

public class Welcome extends JFrame implements ActionListener {

    private JFrame frame;

    // Launch the application.
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Welcome window = new Welcome();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Create the application.
    public Welcome() {
        initialize();
        frame.pack();
    }

    //Variable declaration.
    //A bunch of variables that aren't important
    //End variable declaration.

    // Initialize the contents of the frame.
    private void initialize() {
        //TODO comment this out better
        //Create the JFrame and set all of its stuff.
        frame = new JFrame();
        frame.setBackground(Color.BLACK);
        frame.getContentPane().setBackground(lightGrey);
        frame.setIconImage(logo.getImage());
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SpringLayout springLayout = new SpringLayout();
        frame.getContentPane().setLayout(springLayout);     

        //Add a bunch of components that also aren't important
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("employees")) {
            frame.dispose();
            Employees emp  = new Employees();
            emp.setVisible(true);
        }
    }
}

And my code for the second JFrame ("emp"):

public class Employees extends JFrame {

    private JFrame frame2;

    /**
     * Create the application.
     */
    public Employees() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame2 = new JFrame();
        frame2.setExtendedState(MAXIMIZED_BOTH);
        frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

They're both set to EXIT_ON_CLOSE, but it only works if I exit "welcome", not "emp". If anyone has any ideas/solutions, I'm all ears!

Shannon
  • 100
  • 8
  • 1
    You are extending `JFrame` and have a `JFrame` in the same class... Why? That is most likely your problem. You are doing it to the wrong `JFrame`... All you would need is this for any `JFrame` to exit your program `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` – 3kings Sep 05 '16 at 01:37
  • 1
    [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Sep 05 '16 at 01:40
  • 1
    @3kings Aha, got it, thanks so much! I had no idea you weren't supposed to extend JFrame and create a JFrame in the same program (I just started using swing like two days ago... lol) – Shannon Sep 05 '16 at 01:45
  • @Shannon No problem. Goodluck – 3kings Sep 05 '16 at 05:43

0 Answers0