When I add several JPanels to a parent JPanel or JFrame with cardLayout, the size will be set to the biggest JPanel between all of them, I want the JFrame to have the perfect size for each JPanel, each time I flip between the cards, I wanna have the original size of each JPanel, not the size of the biggest one
Share
Yes, CardLayout uses the width of the widest and the height of the tallest. It’s really intended to address the typical “wizard” interface.
None of the standard layouts will do what you’re asking for.
Here’s a link to how I solved a similar issue. In my case it was adding a JPanel with CardLayout to a JScrollPane. It behaves much the same way with the added pain that it was possible to completely scroll the smaller panel completely out of view leaving a blank screen and a confused user.
I haven’t tried this code directly in a JFrame so I don’t know if this will do what you need, but it’s free and you’re welcome to it.