Hi I am using a BoxLayout
to stack JPanel
s on top of each other (BoxLayout.Y_AXIS
), for example if my parent JPanel
is of height 500 pixels and I add two child panels to it both of height 100 pixels. The BoxLayout
stretches them so that together they occupy the the 500px space. Does anyone know how to disable this feature?
BoxLayout is one of the few layout managers that respects the minimum and maximum sizes of a component. So if you want to prevent a panel from stretching you can use:
panel.setMaximumSize( panel.getPreferredSize() );
Use GridBagLayout instead. You have much more control over your UI.
But if you want to use BoxLayout still, and don't want them to stretch, you can check out using invisible component fillers like rigid areas, glue and fillers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With