How to make the vertical gap in a BoxLayout smaller?

Dot NET picture Dot NET · Feb 1, 2013 · Viewed 10.2k times · Source

I've got the following form which uses a vertical BoxLayout and FlowLayout JPanels for rows:

enter image description here

How can I make the huge gap between each row smaller? This is my code:

Answer

camickr picture camickr · Feb 1, 2013

The problem is that the BoxLayout respects the maximum size of the components. Since panels don't have a maximum size each panel increases in height to take up the available space.

Another solution is to determine the maximum size of each panel after you add the components to the panel:

pnlName.setMaximumSize( pnlName.getPreferredSize() );
pnlSurname.setMaximumSize( pnlSurname.getPreferredSize() );
pnlAge.setMaximumSize( pnlAge.getPreferredSize() );