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
|Layout Manager||Honors preferredSize?||Honors minimumSize||Honors maximumSize|
|BorderLayout||Partly (see BorderLayout extra notes)||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|
Use of the Hints
It should be noted that if you find yourself using these properties a lot you are doing something wrong. In practice, if you understand how the layout managers work, you will rarely need to use these. I recommend not using the hints at first, then if needed make some fine-adjustments with the hints.
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. 1. 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.