Updating JFrame size automatically after adding new components

I have a JFrame with a JPanel inside (the default material when using Window Builder Pro). In my code, I add a new JPanel, but the external JFrame cannot resize to accommodate the new components. So I need to drag manually.

Here is a screenshot:

enter image description here

How can I make sure that no matter what I post there, the JFrame automatically updates its size to accommodate new Swing components?

+4
source share
3 answers

After adding a new component, try calling

frame.pack() 

on the attached frame. The pack method sets the frame size depending on the size of its basic components. This is actually a method of the Window class, the documentation available here

If you don't have a link to Window / Frame, a convenient way is to call SwingUtilities.getWindowAncestor () in the panel after it is added.

+5
source

How to achieve this in WindowsBuilder I can’t say, but when you start just call pack on the frame

+2
source

The usual way to modify an existing native window, frame, or dialog box for its contents is pack . See an example here . If you need access to, for example, a shot from a lung of a child, you can get it using

 JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); 
+2
source

All Articles