4

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.

Samsara
  • 57
  • 2
  • 14
Ankush Pruthi
  • 121
  • 2
  • 4
  • 14
  • 2
    Use the `dispose()` method on the frame that you want to close. But using multiple `JFrames` is not recommended, rather look into multiple dialogs or internal frames. –  Sep 21 '13 at 09:57
  • 2
    You may also wish to consider [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice). Using frames for form switching is essentially, really bad design. I would consider using `JPanel`s as my primary container for the application and using either `JTabbedPane`s or `CardLayout` to allow the user to switch between them - IMHO... – MadProgrammer Sep 21 '13 at 10:05

7 Answers7

9

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

Levenal
  • 3,796
  • 3
  • 24
  • 29
3

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();
}
Aleksey Dz
  • 71
  • 4
1

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

Asher_
  • 23
  • 4
0

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
Pang
  • 9,564
  • 146
  • 81
  • 122
Delta3
  • 1
0

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.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
0

If this doesn't work, try this

JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
osgx
  • 90,338
  • 53
  • 357
  • 513
0
  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
    }