2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jayanga Kaushalya
  • 2,674
  • 5
  • 38
  • 58

5 Answers5

6

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.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1, A CardLayout manager should definitely be considered here. – mre Aug 24 '11 at 13:46
  • No I am creating Search box. In main menu(frame) if i click search button it must open search frame and i want to hide Main menu. But if i exit search. I must show again main menu. I cant destroy Main menu object. Because there is some information saved. I want to call that previous object. Which means i want to set its visibility true again. – Jayanga Kaushalya Aug 24 '11 at 13:49
  • 3
    @JKAUSHALYA, It doesn't destroy the component, it simply hides it. In order to show the previous component, a simple invocation of `show(...)` is required. It's really not that difficult. This layout manager does exactly what you want. Perhaps you should read the tutorial linked to you by @mKorbel. – mre Aug 24 '11 at 13:54
  • See the linked (specific) example. – Andrew Thompson Aug 24 '11 at 16:38
3

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

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

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
2

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.

ring bearer
  • 20,383
  • 7
  • 59
  • 72
  • No I am creating Search box. In main menu(frame) if i click search button it must open search frame and i want to hide Main menu. But if i exit search. I must show again main menu. I cant destroy Main menu object. Because there is some information saved. I want to call that previos object. Which means i want to set its visibility true again. – Jayanga Kaushalya Aug 24 '11 at 13:46
  • @JKAUSHALYA I'm not mind readers, and if I remember lots of times I made mistakes on basic stuff, for reall help you have to edit your post with the runnable code, that shows your issue, otherwise anything would be only shots to the dark – mKorbel Aug 24 '11 at 13:53
0

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.

Hassan Zia
  • 330
  • 5
  • 17