-1

I have read similar topics but i did find answer there.

I created JFrame with close button. After click I want to close current window. I try setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE), or setVisible(false).

public class Windows {
 JFrame frame;
 JFrame frame1;


public Windows(){

} 


 public JFrame getCreateFrame(){

  frame1 = new JFrame("Create User");
  frame1.setSize(500,500);
  frame1.setVisible(true);
  frame1.getContentPane().add(new Panels().getwelcomTxtLabelPanel1(), BorderLayout.NORTH);
  frame1.getContentPane().add(new Panels().getCreateUser(), BorderLayout.SOUTH); 
  frame1.getContentPane().add(new Panels().getUserLabel(), BorderLayout.WEST);
  frame1.getContentPane().add(new Panels().getUserField(), BorderLayout.CENTER);
  return frame1;
     }
  }

Here is a button.

public JButton getCancelButton(){

 cancel = new JButton("cancel");
 cancel.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e){
          new Windows().getCreateFrame().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

              }
         });
return cancel;
     }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
krychuq
  • 335
  • 3
  • 9
  • 22
  • 1
    And what's not happening as expected? – Mordechai Apr 22 '15 at 19:42
  • it's disappears just 0,01s hard to see it even. It's works when i press exit – krychuq Apr 22 '15 at 19:45
  • 1
    The whole structure of your code is wrong. You need to start over. Start with the `ButtomDemo.java` code found in the Swing tutorial on [How to Use Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html). Remove two of the buttons and rename the other to "Exit" and then add your exit code to the ActionListener. The tutorial shows you how to better structure your code and use invokeLater() to create the GUI on the Event Dispatch Thread. – camickr Apr 22 '15 at 20:00
  • It's hard to tell, but I'm guessing, you keep creating frames, rather then maintains a reference to the frame to which y want to work with – MadProgrammer Apr 22 '15 at 21:37
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 23 '15 at 01:46

2 Answers2

2

You have to make the frame invisible and dispose it.

JFrame frame;

frame.setVisible(false);
frame.dispose();

This completely closes the frame. If only this frame is open and no nondeamon threads are running, the program will quit after disposing the frame.

  • it's not working with frame.setVisible(false); and frame.dispose(); – krychuq Apr 22 '15 at 19:51
  • `You have to make the frame invisible and dispose it.` you don't need to make the frame invisible first. All you need to do is invoke dispose() on the frame. – camickr Apr 22 '15 at 19:56
  • you create a new window every time you call 'getCreateWindow()'. You have to close window that is already visible. –  Apr 22 '15 at 19:58
  • I left just frame.dispose(); and it doesn't work. – krychuq Apr 22 '15 at 19:59
  • @krychuq, your code has a terrible structure. Delete it and start over. It is better to start with a working example and customize it than it is to try to modify poorly structured code. – camickr Apr 22 '15 at 20:02
2

The problem is the following action (and not only this):

cancel.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e){
      new Windows().getCreateFrame().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
   }
});

here you create a new Windows object and call getCreateFrame() which creates a new JFrame and then you call setDefaultCloseOperation() on it.

So you work with different Windows / JFrame instances.

Instead you should create your JFrame in the constructor of Windows and call setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE) of this JFrame in the constructor as well.

Afterwards you can use setVisible(false) in your action - but for this JFrame and not for a new created one.

BTW. getCancelButton() should most probably not create a new button every time it is called.

tomse
  • 501
  • 2
  • 7