user3503758
user3503758

Reputation: 77

How to go back to previous frame that was invisible

I am very new to Java Swing and I am working on a login frame with swing that works like this.

After I login successfully in a frame, another new frame is opened while the login frame goes to invisible.

What I am trying to do is that when i close the another frame (after login frame) I want the previous login frame to show again from invisible to visible. please let me know how to do this..:)

Upvotes: 1

Views: 13715

Answers (3)

Manik khosla
Manik khosla

Reputation: 1

Here i am just considering two frames and at present you are at second frame and wanna go back to first frame.

public class previous_action implements ActionListener{
    public void actionPerformed(ActionEvent t){
        Movieticket m;
        m=new Movieticket();
        m.display();
    }
}

Here previous action is a class which will take you back to previous frame.Button frame is a class which sets frame where we are at present.Movie ticket is a public class containing display function which sets frame when the application started that is the first frame. when the button is clicked it will take you to previous frame.

Upvotes: 0

Yubaraj
Yubaraj

Reputation: 3988

Suppose your previous frame is myPreviousFrame

just write myPreviousFrame.setVisible(true); when you want to make visible.

Example:

currentFrame.dispose();
myPreviousFrame.setVisible(true);

Note: if you write code System.exit(0) it will close (terminate) your application. When your application goes terminate you can not make login frame as visible. You need to restart your application. So you need to write dispose().

UPDATED:

I suppose you have a method exitForm() which invokes when you click the Close (X).

Example:

private void exitForm(java.awt.event.WindowEvent evt) {                          
     //System.exit(0); which was used 
     // to fullfill your requirement you need to write below code
     this.dispose();// here [this] keyword means your current frame
     //OR simply you can use this.setVisible(false); instead of this.dispose();
     myPreviousFrame.setVisible(true); // this will displays your login frame
} 

Upvotes: 1

user2575725
user2575725

Reputation:

u may try like this:

public class jFrame1 extends javax.swing.JFrame{
  // ur code
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jFrame2 f2 = new jFrame2(this);
    f2.setVisible(true);
    this.setVisible(false);
  }
}

public class jFrame2 extends javax.swing.JFrame{
  // ur code
  private JFrame frame;

  public jFrame2(JFrame frame) {
    this.frame = frame;
  }

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this.frame.setVisible(true);
    this.setVisible(false);
    this.dispose();
  }

  // so on
}

Upvotes: 1

Related Questions