Jayanga Kaushalya
Jayanga Kaushalya

Reputation: 2744

How to use setVisible in JFrames?

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).

Is there any special method(s) to do this? How can I do it?

Upvotes: 2

Views: 49109

Answers (5)

Hassan Zia
Hassan Zia

Reputation: 330

create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.

gui obj = new gui();

and if you click on back button now than do these also

this.setVisibility(false);
obj.setVisibility(true);

that's all you need.

good luck.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109815

Consider using CardLayout instead of hunting for how many JFrames there are. Then..

  • only one JFrame would be needed
  • any of Next/Back Actions will be only switching between cards

There are lots of examples in this forum - e.g. as shown here.

Upvotes: 6

Andrew Thompson
Andrew Thompson

Reputation: 168825

That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.

Upvotes: 3

ring bearer
ring bearer

Reputation: 20783

You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.

If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.

Upvotes: 2

Heisenbug
Heisenbug

Reputation: 39164

If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.

Upvotes: 2

Related Questions