Java: How to close a JFrame when opening another?

My program starts with an image with a text box in a JFrame. I want when custom types start to close the JFrame picture and open another JFrame with the main program. I tried

processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

on the image frame, but it closes all the windows.

+3
source share
9 answers

The method JFrame.setVisiblecan be used to hide or display the JFrame based on the arguments, but JFrame.disposeactually “destroy” the frame by closing it and freeing the resources that it used. Here you can call setVisible(false)on the image frame if you intend to re-open it, or call dispose()on the image frame if you do not open it again, so your program may free some memory. Then you call setVisible(true)on the main frame to make it visible.

+7
source

Perhaps if you set the default JFrame image to a close operation for something other than EXIT_ON_CLOSEmaybe DISPOSE_ON_CLOSEyou can interfere with your application before the second JFrame appears.

+2
source

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
+2

:

opens_frame frameOld= new opens_frame();
            frameOld.setVisible(true);
            Closing_Frame.setVisible(false);
            Closing_Frame.dispose();
0
private void closeTheCurrentFrameAndOpenNew(java.awt.event.ActionEvent evt){

 dispose();//To close the current window

 YourClassName closeCurrentWindow = new YourClassName();
 closeCurrentWindow.setVisible(true);//Open the new window

}
0

:

public void actionPerformed(ActionEvent e) {
    String userName =  textField.getText();
    String password = textField_1.getText();
    if(userName.equals("mgm") &&  password.equals("12345")) {            
         secondFrame nF = new secondFrame();

         nF.setVisible(false);
         dispose();          
    }   
    else 
    {
        JOptionPane.showMessageDialog(null, " Wrong password ");
    }
}
0

I searched the same and found that using "this" is the best and easiest option. you can use the following code: this.dispose();

0
source

This post is a bit outdated, but nonetheless.

If you initialize the form as follows:

JFrame firstForm = new JFrame();

firstForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
firstForm.setSize(800, 600);
firstForm.setLocationRelativeTo(null);

firstForm.setVisible(true);

And, for example, create or open another form with the button:

JFrame secondForm = new JFrame();

secondForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
secondForm.setSize(800, 600);
secondForm.setLocationRelativeTo(null);

secondForm.setVisible(true);

this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

This will destroy and destroy the first window without exiting the program.
The key is to install setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE).
It also raises events (I tested it with an event WindowClosing).

0
source
if(username.equals("gaffar")&&password.equals("12345"))
    {
    label.setText("Be ready to continue");
    //Start of 2nd jframe
    NewJFrame1 n=new NewJFrame1();
     n.setVisible(true);
     //Stop code for ist jframe
     NewJFrame m=new NewJFrame();
     m.setVisible(false);
     dispose();
    }
-1
source

Source: https://habr.com/ru/post/1785964/


All Articles