Christos Baziotis
Christos Baziotis

Reputation: 6035

How to go back to a JFrames in Java

I have a button in a JFrame, if pressed, it takes us to another frame. I used this code:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

    SecondForm secondform = new SecondForm();
    secondform.setVisible(true);
    setVisible(false);
    dispose();
    }

So the new frame opens and everything is ok. Then i placed another button -in the second frame- in order to go back to the previous frame. I used this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    MainForm Mform = new MainForm();
    Mform.setVisible(true);
    setVisible(false);
    dispose();
}                                        

The thing is, i don't think this is the right way to do this. What i want is to:

Is there a way to do that using the first MainForm instance and not creating a new one every time i want to go back. I monitored my program and every time i go back and forth the frames and as i suspected, the ram being used by it keeps increasing.

Thanks in advance.

EDIT : I have a login system and when the user put the correct credentials a new ManiForm instance is created.

MainForm Mform = new MainForm();
Mform.setVisible(true);

That is the instance i want to use. Ii there a way to make MForm visible again from the secondform?

First of all thanks for the help!

I agree that it is easier not to use more than one JFrames, but can you please tell me which is the better way to do what i asked in the first post? The answer Robin gave me is very nice but i don't know what to put as an argument there*:

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
    *   new SecondForm().setVisible(true);
        }
    });

It's from the auto-generated code from NetBeans.

I tried

new SecondForm(super).setVisible(true);

but i still get compile errors. Apparently i must put super.something() but i don't know what. I tried many but no luck.

Upvotes: 4

Views: 35862

Answers (3)

McLoh
McLoh

Reputation: 79

For swing applications, the standard is to use a STATIC MAIN FRAME, in other words, make your main frame (mframe) static and add methods to pop-up new frames, dialogs, optionPanes, etc. You can even control the visibility of the main frame throw static calls. That's the way you implement a UNIQUE FRAME for your application, even tho you instance other frames for navigation, all child frames can refer to the main frame without the need of passing it as parameter to constructors or creating new instances of it.

The Main Class

`/* to start the application */

public class Main{
    public static MainFrame MFRAME;

    public static void main(String[] args){
        /*use this for thread safe*/
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Main.MFRAME = new MainFrame(/*init parms to constructor*/);
            }
        });
    }
}

`

The Main Frame

`/* to create the main frame */

public class MainFrame extends JFrame{

    public MainFrame(/*your constructor parms*/){
        /* constructor implementation */
    }


    public void openOtherFrame(/*your parms*/){
        OtherFrame oFrm = new OtherFrame(/*your parms*/);
    }

    /* other methods implementation */
}

`

The child frame

`/* to open child frames controling the visibility of the main frame*/

public class OtherFrame extends JFrame{

    public OtherFrame(/*your constructor parms*/){
        /* hide main frame and show this one*/
        Main.MFRAME.setVisible(false);
        this.setVilible(true);
        /* do something else */
        /* show main frame and dispose this one*/
        Main.MFRAME.setVisible(true);
        this.dispose();
    }

    /* other methods implementation */
}

`

Upvotes: 3

MartinL
MartinL

Reputation: 3308

you shouldn't use more then one frame.

You should have NOTHING in JFrame except defaultExitOperation, size, preferedsize and visible true. Instead place all buttons and fields into a JPanel and add/remove the JPanel from the JFrame.

If you want another Window open use JDialog.

btw: you can have your MainFrame setVisible false and open a JDialog with your MainFrame as parent. Only if someone writes down the right user + password you make the MainFrame visible.

Upvotes: 9

Robin
Robin

Reputation: 36601

If you pass your MainForm to the SecondForm class (for example using a constructor parameter) the SecondForm instance can make the original MainForm instance visible again instead of creating a new one.

For example

public class SecondForm extends JFrame{
 private final MainForm mainForm;
 public SecondForm( MainForm form ){
   mainForm = form;
 }
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  mainForm.setVisible(true);
  setVisible(false);
  dispose();
 }  
}

and in your MainForm class

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
 SecondForm secondform = new SecondForm( this );
 secondform.setVisible(true);
 setVisible(false);
 dispose();
}

Upvotes: 1

Related Questions