2

When I start my program I create the MainFrame. When I click on the button "search", I show another JFrame. If it obtains results on the search, the goal is to fill the information in MainFrame.

It's all working. But to be able to display the information, I have to re-create the MainFrame. What means, that the two MainFrame's stay open.

How do I hide the first?


Supose thet i have only the MainFrame and one dialog with the Search form.

When i click on ok button on the search dialog, i have some info.

In the MainFrame Class i know that the info is there.

I have the settext on the textfield's, etc... and on the final line i have the "setvisible(true)".

But they remain blanc! Why??


Can anyone post here an example, how to do it? Please

I just search for all options, but i can't solve.

Pepper
  • 97
  • 1
  • 10

3 Answers3

7

A Swing application should consist of one JFrame maximum. If you need to show multiple "windows", then you can use multiple JDialog and use dispose() to hide the dialog.

Alternatively, you can use a CardLayout as the layout manager instead of having too many dialogs.

See:

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

Use JFrame.setVisible(false) to hide, and JFrame.setVisible(true) to make it reappear. This will work with any JComponent in swing, not just JFrames.

Jon W
  • 593
  • 1
  • 6
  • 15
  • I know that. But when i open the second MainFrame with the information showed, how can i hide the first one? – Pepper Dec 26 '12 at 19:46
  • Call `setVisible(false)` for the first one. However, I'd recommend that instead of using two JFrames, you follow Eng.Fouad's answer. Use one JFrame with multiple JPanels in it, and switch between the panels instead of making new frames. – Jon W Dec 26 '12 at 19:47
  • 1
    @pepper Call `dispose` on the first frame - it would appear that you no longer need it any way, so you might as well get rid of the native resources been consumed by the first frame. Better to use a `JDialog` for the second window and populate the first frame with it's results – MadProgrammer Dec 26 '12 at 19:51
  • Ok, so supose if i have only the MainFrame and one dialog with the Search form. When i click on ok button on the search dialog, i have some info. and in the MainFrame Class i know that the info is there. i have the settext on the textfield's, etc... on the final line i have the "setvisible(true)"...but they remain blanc!! why???? – Pepper Dec 26 '12 at 20:05
  • @Pepper: You should have model classes that hold the data from the JDialog and populate the main JPanel. – Gilbert Le Blanc Dec 26 '12 at 20:40
  • still with the same problem! – Pepper Dec 26 '12 at 22:29
0

You can hide a JFrame with a call to the method:

   setVisible(false);

However, you may consider using JDialogs instead as there you have the option to create a modal dialog which means that the user may only interact with the Search Dialog while it is open.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192