<p>To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action.</p>

<p>So, what's the correct way of doing such a thing? I've tried</p>

<pre><code>panel = new CustomJPanelWithComponentsOnIt();
parentFrameJPanelBelongsTo.pack();
</code></pre>

<p>but this won't work. What would you suggest?</p>

```
```
frame.setContentPane(newContents());
frame.revalidate(); // frame.pack() if you want to resize.
```
```
Remember, Java use 'copy reference by value' argument passing. So changing a variable wont change copies of the reference passed to other methods.
Also note
```
JFrame
```
is very confusing in the name of usability. Adding a component or setting a layout (usually) performs the operation on the content pane. Oddly enough, getting the layout really does give you the frame's layout manager.