Java: How do I close a JFrame while opening another one?
Asked Answered
G

12

5

My program starts with a picture with a textfield in a JFrame. I want when the user types start it closes the picture JFrame and opens another JFrame with the main program. I've tried

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

on the Image frame but it closes all the windows.

Gardening answered 17/1, 2011 at 18:15 Comment(0)
A
9

The method JFrame.setVisible can be used to hide or display the JFrame based on the arguments, while JFrame.dispose will actually "destroy" the frame, by closing it and freeing up resources that it used. Here, you would call setVisible(false) on the picture frame if you intend to reopen it, or call dispose() on the picture frame if you will not be opening it again, so your program can free some memory. Then you would call setVisible(true) on the main frame to make it visible.

Atal answered 17/1, 2011 at 18:20 Comment(0)
F
3

you also can use this code

for example

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Factorial answered 21/1, 2011 at 20:57 Comment(0)
C
2

Maybe if you set the picture JFrame's default close operation to something besides EXIT_ON_CLOSE, perhaps DISPOSE_ON_CLOSE, you can prevent your application from closing before the second JFrame appears.

Cluck answered 17/1, 2011 at 18:22 Comment(0)
M
1

Here is my solution to this problem:

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 ");
    }
}
Mobcap answered 30/7, 2015 at 15:25 Comment(0)
E
1

I was searching for the same thing and found that using "this" is the best and easiest option. you can Use the following code: this.dispose();

Equities answered 17/10, 2017 at 17:19 Comment(0)
D
1

This post is a bit old but nevertheless.

If you initialize the form like that:

JFrame firstForm = new JFrame();

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

firstForm.setVisible(true);

And for instance create or open another form by a 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 dispose and destroy the first window without exiting the program.
The key is to set setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE).
It also raises the events (I've tested it with the WindowClosing event).

Dues answered 15/11, 2017 at 9:6 Comment(0)
G
1

For netbeans use the reference of the current Object and setVisible(false); for example

private void submitActionPerformed(java.awt.event.ActionEvent evt)
{                                
    // TODO add your handling code here:
    this.setVisible(false);//Closing the Current frame
    new login().setVisible(true);// Opening a new frame
}                                     
Glade answered 23/10, 2018 at 16:57 Comment(0)
H
0

you also can use this :

opens_frame frameOld= new opens_frame();
            frameOld.setVisible(true);
            Closing_Frame.setVisible(false);
            Closing_Frame.dispose();
Hammerskjold answered 1/7, 2014 at 1:48 Comment(0)
S
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

}
Shooter answered 20/11, 2014 at 15:35 Comment(0)
L
0

First call it

new Window().nextjframe.setVisible(true);
thisjframe.setVisible(false);
Likker answered 16/1, 2022 at 6:7 Comment(0)
A
-1
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();
    }
Ammonia answered 14/8, 2016 at 18:23 Comment(0)
L
-1

This is what i came up for opening a new jframe while closing the other one:

JFrame CreateAccountGUI = new JFrame();
CreateAccountGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CreateAccountGUI.setSize(800, 600);
CreateAccountGUI.setLocationRelativeTo(null);
CreateAccountGUI.setVisible(true);   
this.setVisible(false);
Lazor answered 3/9, 2022 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.