Reputation: 15257
I have a JFrame with a JPanel inside (default stuff when using Window Builder Pro). In my code I am adding a new JPanel, but the outer JFrame fails to re-size to accomodate for the new components. So I have to drag manually.
Here is a screenshot:
How can I make it so that no matter what I put in there, the JFrame automatically updates its size to accommodate for the new Swing components?
Upvotes: 4
Views: 2329
Reputation: 46970
The usual way to reshape the enclosing native window, frame, or dialog to fit its contents is pack
. See the example here. If you need access to e.g. a frame from a lightweight child, you can get it with
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
Upvotes: 2
Reputation: 3068
After adding the new component, try calling
frame.pack()
on the enclosing frame. The pack method will set the size of the frame based on the size of its underlying components. This is actually a method of the Window class, documentation available here
If you don't have a reference to the Window/Frame handy, an easy method is to call SwingUtilities.getWindowAncestor() on the panel after it has been added.
Upvotes: 5
Reputation: 347294
How to achieve this in WindowsBuilder I can't say, but when running, simply call pack
on the frame
Upvotes: 2