I have a JFrame, which is the toplevel container for a window, and a single JComponent (that I have implemented) which is contained in the JFrame.
In part of my JComponent’s logic, I need to resize the window to a specific size, but now matter what I try, it has not worked. I have tried calling setPreferredSize and setSize on the JFrame, the JComponent, and on the Container returned from calling getParent() on the component.
I have a feeling this has something to do with not calling pack after changing the size, but I cannot figure out how to access the JFrame from the JComponent to call pack on it (I am no swing expert).
Thank you
You can always get the top level window by calling the
SwingUtilities#getWindowAncestormethod:passing in your component. Just make sure that you only call this after the GUI has been rendered, else you’ll get a nasty NullPointerException.