-3

Need help!

In java swing, how can I jump back(open up) the hidden form from which the previous form had been generated?

Elaborating -> I made an object of form2 in form1, hid the 1st form and displayed the 2nd one. Now after a while I have to hide the form2 and open the original form1. How can I do that?

  • 1
    What exactly are `form2` and `form1`? – Bhesh Gurung Apr 07 '14 at 14:23
  • 1
    Form1 is a class in which I made an object of Form2 on a button click. I used setVisible to hide Form1 and then open Form2. Now Form2 is visible. Then on a button click on Form2, I need to dispose Form2 and unhide Form1. I don't know any function to unhide Form2, neither do I want to create a new object of Form1. – Snehil_V127 Apr 07 '14 at 14:28

2 Answers2

2

You can use form.setVisible(true) and form.setVisible(false)

In form1 you'll put this under the button

form2 f2 = new form2(this);

In form2 you'll do this

f1.setVisible(false);

then when you go back

f1.setVisible(true);
this.dispose();
Leniaal
  • 1,431
  • 2
  • 13
  • 23
0

Unless you have a really good reason to do something like this, then don't.

Instead, use a CardLayout which will allow you to move back and forward and set the currently active view at your hearts content

Check out How to use CardLayout and The Use of Multiple JFrames: Good or Bad Practice? for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366