Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoxLayout stretches component to fit parent panel

Hi I am using a BoxLayout to stack JPanels 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?

like image 359
Aly Avatar asked Feb 03 '10 19:02

Aly


2 Answers

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() );
like image 51
camickr Avatar answered Oct 19 '22 14:10

camickr


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.

like image 11
Ascalonian Avatar answered Oct 19 '22 13:10

Ascalonian