Swing Layout Managers

To get things to size and layout correctly in Swing you need to know which of the sizing hint methods that each layout manager honors. Here is the table of information from the original blog post which can be found here: http://thebadprogrammer.com/2012/04/swing-layout-and-sizing/


Layout Manager Honors setPreferredSize()? Honors setMinimumSize()? Honors setMaximumSize()?
BorderLayout Partly (see BorderLayout extra notes) No No
GridBagLayout Yes Yes No
FlowLayout Yes No No
CardLayout Sort of (see Card and GridLayout extra notes) No No
BoxLayout X_AXIS–honors preferred width
Y_AXIS–honors preferred height
Yes Yes
GridLayout Sort of (see Card and GridLayout extra notes) No No
GroupLayout Sequential Groups–Yes
Parallel Groups–Sort of (see GroupLayout extra notes)
Sequential Groups–Yes
Parallel Groups–No
Sequential Groups–Yes
Parallel Groups–No

BorderLayout Extra Notes

  • NORTH and SOUTH components — respects component’s preferred height if possible, width is set to full available width of the container
  • EAST and WEST components — respects component’s preferred width if possible, height is set to full available height of the container
  • CENTER component — gets whatever space is left over, if any

CardLayout and GridLayout Extra Notes

Both CardLayout and GridLayout sizes all of the components in a container it is controlling to be the same size. It searches for the component with the largest preferred width and the component with the largest preferred height and sets all components to that preferred width and height. It should be noted that the maximum preferred height and width DO NOT have to come from the same component.

GroupLayout Extra Notes

Note that GroupLayout is really designed to be used by GUI builders. It can be hand coded but that wasn’t really what it was designed for. Anyway, parallel groups have some odd sizing rules and it is best to read about them straight from Oracle, (from:http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html )

The underlying mechanism works as follows:
1. The size of the parallel group is set to the preferred size of the largest element; so to the preferred size of c4 in our example.
2. Resizable elements are stretched to the size of the group. In our example, only c3 is effectively stretched, the size of c4 already corresponds to the size of the group.

There are currently no comments highlighted.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>