2

I have a program where I can click on a button and it calls in a new panel, making the current one false through action listener. This panel uses up the whole size of the JFrame, giving me a form to fill out and buttons to press.

Is this the correct method to do this? Or is there a better way as I am running into bugs where the newly called JPanel appears on other JPanels. I could use setVisible(false) but then certain elements such as buttons do not appear.

Endery
  • 1,090
  • 17
  • 31
Bob
  • 41
  • 1
  • 11
  • 6
    the proper way would be to use a proper Layout in my eyes. Since it seems that you want to "replace" the panel that is visible to the user the [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) might be fitting your needs. – SomeJavaGuy Mar 08 '16 at 14:47
  • The correct "method" to do this would pretty much depend on your goals. Should something else be visible? Should you not use a new JFrame instead? Should you be able to use all panels at the same time? Ask yourself these questions first. – Kalin Varbanov Mar 08 '16 at 14:57
  • @KalinVarbanov How do I use multiple JFrames? – Bob Mar 08 '16 at 14:59
  • 3
    See also [*The Use of Multiple Frames…*](http://stackoverflow.com/q/9554636/230513) – trashgod Mar 08 '16 at 15:47

1 Answers1

3

According to what you want, it appears to me you would probably want to look at CardLayout where you can swtich 2 or more panels in the frame.

Conceptually, each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:

  • By asking for either the first or last card, in the order it was added to the container
  • By flipping through the deck backwards or forwards
  • By specifying a card with a specific name
Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106