-1

So I have two JFrames. If I press a button I want to make the main frame disappear, and the new frame to appear. But when I close the new frame, I want to make the old frame reappear. The first part I cant do it with a simple:

mainFrame.setVisible(false);
newFrame.setVisible(true);

But I want to make the main frame reappear again after I close the new frame. I use DISPOSE_ON_CLOSE on the new frame by the way.

Mike G
  • 4,232
  • 9
  • 40
  • 66
Altair2033
  • 21
  • 8
  • And what happens now? Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 17 '17 at 19:35
  • The code is too long to post. But I don't know what you don't understand. We have two frames, the main frame and the new frame. When you run the application the main frame is visible only. If I press a button the main frame is not visible anymore, and the new frame appear. If I exit the new frame, I want to make the main frame reappear. – Altair2033 Jan 17 '17 at 19:44
  • Also to complement on @MadProgrammer's comment, you should also read [The use of multiple JFrames, Good/Bad practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (BAD), instead you might want to use [`JDialog`](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)s or a [Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Frakcool Jan 17 '17 at 19:45
  • Have you looked into adding a [WindowListener](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#addWindowListener-java.awt.event.WindowListener-)? – VGR Jan 17 '17 at 19:46
  • 1
    *"The code is too long to post"* We don't want your whole code, please read how to make a [mcve] or a [Short, Self Contained, Correct Example](http://sscce.org/) that demonstrates your issue, it should be short enough to be posted here and complete including imports and a `main` method. – Frakcool Jan 17 '17 at 19:47
  • Also you could try using [`JFrame#HIDE_ON_CLOSE`](https://docs.oracle.com/javase/7/docs/api/javax/swing/WindowConstants.html#HIDE_ON_CLOSE) constant or [`JFrame#DO_NOTHING_ON_CLOSE`](https://docs.oracle.com/javase/7/docs/api/javax/swing/WindowConstants.html#DO_NOTHING_ON_CLOSE) and add your logic. – Frakcool Jan 17 '17 at 19:50
  • Thank you for answers. @VGR, using the WndowListener it worked. Thank you very much. – Altair2033 Jan 17 '17 at 19:54

1 Answers1

0

Add a WindowListener to the new JFrame and override the WindowClosing method, then set the mainFrame visible again.

newFrame.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        mainFrame.setVisible(true);
    }
});

Note that both Frames need to be set to JFrame.DISPOSE_ON_CLOSE.

Vinicius Victor
  • 348
  • 1
  • 10