Close current window and pop out previous (windowListener)

608 Views Asked by At

I have a server/client app. When the system is started a login window pops out. When Connect clicked - another JFrame (the Game frame) pops out. What I want is: when the 'X' on the Game frame is clicked - the game, for the current user, to be stopped and the login window to pop-out back again.

In short - when 'X' is clicked - terminate current process, close the window and pop-out the previous window. What is left is just to implement the 'windowClosing()' method, but I don't have a clue how. Here's what I have:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;

    public class GameplayFrame extends JFrame {

    private GECanvas canvas;

    public GameplayFrame() {
        super("BattleCity");
        canvas = new GECanvas();
        getContentPane().setLayout(new BorderLayout());
        canvas.setPreferredSize(new Dimension(600, 600));
        getContentPane().add(canvas, BorderLayout.CENTER);
        setResizable(false);
        pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width - getSize().width) / 2;
        int y = (dim.height - getSize().height) / 2;
        setLocation(x, y);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new NaBabaMiShortite());
    }

    private class NaBabaMiShortite extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {

                // TO-DO
        }
    }

    public GECanvas getCanvas() {
        return canvas;
    }
}
1

There are 1 best solutions below

2
On

Don't terminate the process. Simply call setVisible() on the two windows to make them visible or hide them as necessary.