Basic Layout Components in Java


All of the Components in the above example need some form of layout (except for the Menu and MenuBar). The layout determines how the components will be arranged on the screen. A Container's layout is controlled by a LayoutManager. Java has a pretty good variety of LayoutManagers to choose from:

Name What it is
BorderLayout Each component is placed against an edge or in the center as specified by "North," "South," "East," "West" or "Center"
CardLayout Only displays a single component at a time. Methods are available for cycling through the components
FlowLayout Arranges the components in rows from left to right, filling each row as much as possible before starting a new row.
GridBagLayout The most complex of the layouts. It works rather similarly to tables in HTML
GridLayout Lays out the components into cells in a grid, one Component per cell. You have to specify the height and/or width of the layout in cells.