0

i have 5 jFrames in my java project. And i want to make like a Main Menu.

I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.

It is possible? im programming with java jdk 8 and netbeans. Thanks

Edit: I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
MK_03
  • 59
  • 7
  • Also please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) before moving forward with this design. – Hovercraft Full Of Eels Jul 10 '16 at 16:29
  • I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated – MK_03 Jul 10 '16 at 16:37
  • @HovercraftFullOfEels You've misunderstood. The asker isn't looking to open a new JFrame, but change the contents of the current one as though it was a new one. – Nic Jul 10 '16 at 16:41
  • Sorry. You want to swap JPanels, and I'll find another duplicate, one that uses CardLayout. Either way, this question has been asked/answered multiple times. – Hovercraft Full Of Eels Jul 10 '16 at 16:41
  • @QPaysTaxes: correct. Here are [better links](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+swing+swap+jpanels) – Hovercraft Full Of Eels Jul 10 '16 at 16:42
  • Thats all i want to know. Thanks for the links – MK_03 Jul 10 '16 at 16:46
  • @HovercraftFullOfEels I haven't used Swing in a long while, so I'm not sure which of those would be best. I'm gonna go with [this](http://stackoverflow.com/q/17727706/), because the answer says how to do it pretty explicitly. Look good to you? – Nic Jul 10 '16 at 16:51
  • Yes, im testing with that answers. Thank you for the help. – MK_03 Jul 10 '16 at 16:54
  • @QPaysTaxes: the best answer would be one that uses [CardLayout]() to assist in the swapping. The [CardLayout tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Hovercraft Full Of Eels Jul 10 '16 at 16:57
  • Possible duplicate of [How to replace JPanel with another JPanel](http://stackoverflow.com/questions/14874613/how-to-replace-jpanel-with-another-jpanel) – Nic Jul 10 '16 at 17:00
  • @HovercraftFullOfEels Sorry, I linked the wrong one. There isn't actually a very good answer for this question, so it might be good to write a canonical answer. – Nic Jul 10 '16 at 17:01

1 Answers1

1

i have 5 jFrames in my java project.

And that's a problem.

And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.

Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:

// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();

// get its contentPane 
Container newContentPane = newJFrame.getContentPane();

// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);

// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off

But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.

Other useful links:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373