I'ld like to add a JPane to a JFrame. The JFrame is empty but the JPane is already filled with components and already has a certain size. I'ld like to add the pane to the frame but the pane has to maintain its size. How do I know what size to make the frame to not resize the panel?
Is this possible without needing complex code for retrieving the sizes of border, insets, title bars and such? My idea was to: 1) remember the size of the panel 2) set the pane to that size (which is too small) 3) add the panel to the frame's content pane. This should resize the panel as the layout manager will make it fill the entire content pane. 4) Retrieve the panel's new size and determine the difference from its original size. 5) Use the found difference to enlarge the frame with thereby restoring the frame's size. The panel should have its original size now without needing to know what kind of other elements are taking up space. Unfortunately I've nog been able to get this working.
I notice that setting the bounds of the frame doesn't resize the frame's content pane instantly. Even not after a call to doLayout(). Same thing with adding the pane to the frame. This is not behavior I expected. Please help!
This would be the method in the code above that makes it do what you're asking:
public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.