-1

Sorry if I might mix terms up. So I have a basic application where I would press the button and Jpanel1 with a label in it, would then switch/replace to JPanel2 that'll have a picture in it, all within the panel. enter image description here

The JPanel inside the box would change from Jpanel1 to JPanel 2 after pressing the button. So is their a way to instance a JPanel in a panel or JFrame? I can't find the method on how to fill the panel with the JPanel.

c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 24 '20 at 00:22
  • See [CardLayout Demo](https://stackoverflow.com/a/46013230/3992939) – c0der Jun 28 '20 at 05:46

3 Answers3

1
frame.getContentPane().add(panel, /*layout.location*/);

You mean add

정지원
  • 37
  • 6
1

The exact way to do this depends on the LayoutManager you use. I suggest checking out the Visual Guide to LayoutManagers. For example, if you use a BorderLayout, you can add a panel to the center and then replace it with a different panel when the user clicks a button.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

I ended using layeredPane with a card layout. Then I placed a panel using the center Border Layout, then instanced the JPanel as the panel from a different class.

Picture of Windowbuilder layout

    panelPPBrowser = new JPanel();
    layeredPane.add(panelPPBrowser, "name_216881785358769");
    panelPPBrowser.setLayout(new BorderLayout(0, 0));
    
    panelBrowser panelBrowserCon = new panelBrowser();
    panelPPBrowser.add(panelBrowserCon, BorderLayout.CENTER);