1

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..:)

user3503758
  • 77
  • 1
  • 1
  • 9
  • The short answer would be, don't. Take a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) for more detail. The longer answer would be to use a CardLayout which would allow you to switch between views on a single window – MadProgrammer Apr 11 '14 at 09:27

3 Answers3

1

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
} 
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
  • I know how to make it visible. But how can I implement the code that runs when I close the current frame please? – user3503758 Apr 11 '14 at 09:17
  • what should i do if i want your two line codes to run when i click the close (x) button on the top of frame please? – user3503758 Apr 11 '14 at 09:52
  • When you click the close (x), this is executing System.exit(0). This means your application terminated. So your login frame is not displaying. You need to replace System.exit(0) by like I suggested code. If you did not get idea where to place code like I suggested. Please show your code. Then I will tell you. I updated my answer see... – Yubaraj Apr 11 '14 at 10:24
1

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
}
0

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.