How to set a JFrame size to fit the CardLayout displayed JPanel?
I have a JFrame containing a set of JPanels in a CardLayout . Each JPanel has a different size and I want the JFrame to adapt to the size of the currently displayed JPanel (not the JPanel to adapt to the size of the JFrame ). How can I achieve this? The general is: if you have a layout problem, always solve it with an appropriate LayoutManager. Never tweak a component's sizing hint to reach your goal. In this case, it's particularly easy to adjust the CardLayout. By default, it calculates its prefSize to the max of prefSizes of all cards. Simply subclass and implement to return the prefSize