Java Programming

Layout Managers

 

Layout managers are responsible for positioning and sizing components within a container.  There are a number of predefined layout managers in the Java Development Kit (JDK).  Each one does the sizing and positioning of components in different ways.  To change the layout manager that a container uses you call the container's setLayout() method.

 

·                    GridLayout arranges components in a grid within a container.  All cells in the grid are the same size.  The GridLayout manager ignores the preferred size of each component and resizes them to fit the cell size.  As each component is added to the container, the GridLayout manager inserts it across (left to right) each column until that row is filled and then goes to the first column of the next row.  When you set a GridLayout you can specify how many rows and/or columns you want in the grid.  The number of columns is calculated by the number of rows and the number of components added.  If you want to specify the number of columns you must not specify the number of rows.

 

·                    FlowLayout arranges components within the container from left to right.  When the container runs out of room on the current row, FlowLayout positions the next component on the next row.  The FlowLayout manager respects the preferred size of each component.  Note that when you resize the container the FlowLayout manager will rearrange the components within the container.  FlowLayout is the default layout manager for applets and Panel containers.

 

·                    BorderLayout divides the container into five areas.  These areas are NORTH, SOUTH, EAST, WEST, and CENTER.  When adding components to a container using a BorderLayout manager you must specify which of the five areas you are adding it to.  You can add only five components to a container using BorderLayout but these components can be containers which can hold multiple components themselves.  When you add a component to NORTH or SOUTH (top or bottom) areas the container resizes the component to fill the entire width of the container but the preferred height of the component is maintained.  When you add a component to WEST or EAST (left or right) areas the container resizes the component to fill the entire height of the container but the preferred width of the component is maintained.  When you add a component to the CENTER area of the container the component will be resized to fill whatever space is left over after the other four areas have positioned their components.  The preferred size of a component placed in the CENTER area is ignored completely.

 

·                    GridBagLayout allows the placement of components that are aligned either vertically or horizontally without constraining those components to be the same height or width.  It does this by defining a dynamic grid of cells.  The constraints object that is passed when the component is added to the container determines how many cells the component uses.  The GridBagLayout is the most flexible of all the layout managers but is also the hardest to master.

 

·                    CardLayout treats each component like a card in a deck of cards.  Only one card is visible at any given time.  The component that is added first is the one that will be visible when the container is first displayed.  You can call built-in methods such as next() to bring a different card to the top of the deck so it is visible.