Ankush Pruthi
Ankush Pruthi

Reputation: 121

Closing jFrame after clicking JButton

I have designed two JFrames in NetBeans.

When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want). In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.

In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.

The scenerio is JFframe1 -> JFrame2 -> JFrame1

Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.

Upvotes: 4

Views: 99947

Answers (7)

Asher_
Asher_

Reputation: 23

Well, if you already have a actionListener, you should add this:

JFrame1.dispose(); // This will close the frame

The JFrame1 is the name of your frame. And if you want to open another frame that you have, add this:

JFrame2.setVisible(true); // This will put the other frame visible

Upvotes: 1

user3053416
user3053416

Reputation:

  1. Have a MainClass with a main() method.
  2. Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
  3. After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.

Example:

    //btn event inside 1st JFrame/window
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         MainProgram.openResultsForm();  //MainProgram opens 2nd window
         MainProgram.queryEntryForm.dispose();   //MainProgam closes this,
                                                 //the 1st window
    }

Upvotes: 0

Guermou Di Omar
Guermou Di Omar

Reputation: 1

If this doesn't work, try this

JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();

Upvotes: 0

Gunshield_XD
Gunshield_XD

Reputation: 1

this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):

RegScreen.this.setVisible(false);

new MainScreen().setVisible(true);

Hope that this helps :) Regscreen was the original frame open at startup.

Upvotes: 0

Delta3
Delta3

Reputation: 1

I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.

//this is the code for the "cancel" button action listener 
public void actionPerformed(ActionEvent e) {
    setVisible(false);//hides the second JFrame and returns to the primary

Upvotes: 0

Oleksii Dz
Oleksii Dz

Reputation: 71

Somethig like this should be on the constructor or method which create JFrame2:

btnCancel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //call another method in the same class which will close this Jframe
        CloseFrame();
    }
});

It's method which should close JFrame2

public void CloseFrame(){
    super.dispose();
}

Upvotes: 3

Levenal
Levenal

Reputation: 3806

Assuming your button has an actionListener, after clicking the "rules button" put in:

      JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame

And then reverese them for the opposite reaction

Upvotes: 9

Related Questions