0

How can I code a button that, when clicked, closes the current JFrame and opens a new one?

This is what I have so far, but the old frame stays open:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    practise1 s = new practise1();
    s.setVisible(true);
} 

I have tried using .close() after the first { but it gives me an error.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steven
  • 21
  • 1
  • 1
  • 1

5 Answers5

2

If you plan on using the originial JFrame later, use setVisible(false) on the original JFrame. If you plan on closing the first JFrame and never reusing it, you can use dispose().

sneelhorses
  • 175
  • 1
  • 6
  • 1
    Sometimes it is better to guide the OP on the correct path than answer their exact question. This is one of those times. As it is, this advice is driving them further down a path they should not follow, and will cause many problems. – Andrew Thompson Mar 04 '15 at 17:51
0
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource () == button)
    {
      test = new JFrame();
      test.setSize(300,300);
      test.setVisible (true);
      this.dispose();

    }
  }

Dispose AFTER creating the new Frame.

Raildex
  • 3,406
  • 1
  • 18
  • 42
  • Sometimes it is better to guide the OP on the correct path than answer their exact question. This is one of those times. As it is, this advice is driving them further down a path they should not follow, and will cause many problems. – Andrew Thompson Mar 04 '15 at 17:51
  • OP asked for a solution for mutliple JFrames. Of course multiple Frames aren't the best solution. And I'm sure OP is not a specialist with GUIs, so why not let OP play around with multiple Frames? Sooner or later OP will notice, that multiple Frames are too difficult to maintain. – Raildex Mar 05 '15 at 05:21
  • If the OP asked how to shoot themselves in the foot. Would you have helped out? – Andrew Thompson Mar 05 '15 at 07:49
0

Thanks for the help everyone. I got it working using the this.dispose(); method

Steven
  • 21
  • 1
  • 1
  • 1
0

Lets say current Frame is FirstFrame and clicking on JButton goes to NewFrame

import javax.swing.*;

public class FirstFrame extends Jframe implements ActionListener{


  JButton button;    

  public FirstFrame(){
   setVisible(true);
   setSize(500,500);

    button=new JButton("Click me");
    button.addActionListner(this);
   add(button);     
  }

  public static void main(String[] args)
  {
   new FirstFrame();
  }

  public void actionPerformed(ActionEvent e)
   {
   if(e.getSource()==button)
    {
        NewFrame nf=new NewFrame();    // Clicking on the Button will OPEN new Frame in NewFrame.java file 
        dispose();  //this method will close the FirstFrame 
     }
   }


}
Parth Pithadia
  • 276
  • 1
  • 3
  • 18
0

you just put this in your code :

(the exemple here i JButton to do this with the ActionPerformed method)

/**********************************************************************/

private void openBTNActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        dispose();
        FrameTarget t = new FrameTaregt();
        t.setVisible(true);
        //set the size : 1250 pixels de width and 720 pixels de height
        t.setSize(1250, 720);
        //make the frame in the center wuth this 
        t.setLocationRelativeTo(null);
        t.setResizable(true);     

}