-1

I have a main JFrame window with a JButton. When I click on the button a new window is opened, but when I want to close the second window, both window are closed! I want the first to opened all the time. Is there a way to only close the second window? Preciate some help! Thanks!

EDIT: I add new code to show my problem. Class GUI1 has a main frame and a button to open a second frame, GUI2, that has has a button to close GUI2 frame. It's the closing part of GUI2 I can't solve. The code is simple and just for testing.

GUI1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends JFrame implements ActionListener{
JButton btn1;
Container contentPane;
public GUI1()
{
    setTitle("GUI 1");
    setResizable(false);
    setSize(600,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn1 = new JButton("Open GUI 2 frame");
    contentPane.add(btn1);
    btn1.setFocusable(false);
    btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn1)
    {
        GUI2 frame2 = new GUI2();
        frame2.setVisible(true);
    }
}
public static void main(String[] args) {
    GUI1 frame = new GUI1();
    frame.setVisible(true);
}
}

GUI2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI2 extends JFrame implements ActionListener {
Container contentPane;
JButton btn2;
public GUI2()
{
    setTitle("GUI 2");
    setResizable(false);
    setSize(400,200);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn2 = new JButton("Close GUI 2 frame");
    contentPane.add(btn2);
    btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn2)
    {
        // Close GUI2 ??
    }
}
}
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
  • you should probably be using the constant instead of `3` for the default close operation, to make it more clear. is 3 exit on close? – DHall May 28 '12 at 12:07
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) It is `JFrame.DISPOSE_ON_CLOSE`, but **see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556)** – Andrew Thompson May 28 '12 at 12:07
  • 1
    Oh, and don't use 'magic numbers' - I must admit I did not have to glance at the snippet before guessing the problem. – Andrew Thompson May 28 '12 at 12:09

3 Answers3

2

After a second read, I agree with others when they say you shouldn't be creating and discarding JFrames (or using multiple JFrames at all). But if you really want to go that route, I'd suggest:

  • Create a single JFrame, and store a reference to it somewhere your buttons can access;
  • Show the frame when the "add" button is pressed;
  • Use JFrame.HIDE_ON_CLOSE instead of 3 (exit on close) - don't use magic numbers;
  • Hide the frame when the other button is pressed.

Update: the steps to apply the above suggestions to your architecture are:

  • add a field to your GUI1 class - private GUI2 frame - and only create it once (in the constructor of GUI1 for instance);
  • in your actionPerformed, only use frame.setVisible(true) - since the frame was already created;
  • I believe you could keep your DISPOSE_ON_CLOSE on GUI2, if you wanted, but HIDE_ON_CLOSE is more appropriate;
  • since it's GUI2 - the JFrame - who is implementing the action listener, you already have a reference to it: this! Use this.setVisible(false) or simply setVisible(false).
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • OK, Nice! But if I would like to close the second frame with a JButton? – 3D-kreativ May 28 '12 at 12:10
  • 1
    So close it with a button. On action, call `theFrame.dispose()`, but in case you did not hear it before, ***..See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556)*** – Andrew Thompson May 28 '12 at 12:11
  • But my button is in the second frame, and the reference for the second frame is in the class of the first frame. How do I call theFrame.dispose() from the second window? – 3D-kreativ May 28 '12 at 12:30
  • @3D-kreativ I can't answer that because I don't know your architecture. Where is: a) the code that creates that button? b) the code that adds a listener to that button? c) the code that add that button to the frame? d) the code the listener executes? At least (c) will have a reference **both** to the button and the frame. But in the end it's up to you to decide the best way to make the frame reference accessible to the button. – mgibsonbr May 28 '12 at 13:17
  • @3D-kreativ check the update answer, the reference for the second window is simply `this`. – mgibsonbr May 29 '12 at 12:36
  • Thanks for suggestions in your update. I'm new to Java and GUI and I find it hard sometimes to understand it all and it's hard to know if it's a good or bad practise. Things can be done in so many ways. And the things you learn about Java and GUI from the Java course and the book are very basic. Create GUIs are fun and I wish I knew more and could do some more advanced things or find more and better ways to solve tasks like this. But I guess that will come in the future. I will have a look at CardLayout. – 3D-kreativ May 30 '12 at 07:12
2

I have a main JFrame window with a JButton. When I click on the button a new window is opened, but when I want to close the second window, both window are closed!

1.use CardLayout rather than to create a bunch of JFrames on the runtime, really very bad concept, these Objects never gone from UsedMemory, nor be GC'ed

2.use HIDE_ON_CLOSE instead of DISPOSE_ON_CLOSE

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Change :frame2.setDefaultCloseOperation(3); to frame2.setDefaultCloseOperation(DISPOSE_ON_CLOSE); and it should work

John Snow
  • 5,214
  • 4
  • 37
  • 44