Controls and layout managers
Layout managers
Summary
- Each container has a layout manager, which is responsible for arranging the components in a container.
- The container's setLayout method can be used to set a layout manager.
- Certain types of containers have default layout managers.
- The layout manager places the components according to the layout manager's rules, property settings and the constraints associated with each component.
- Each layout manager has a particular set of rules specific to that layout manager.
Examples of the Box and Grid layouts
Note
- The Box and Grid layout managers automatically resize each component to fill the available space. To avoid unattractive panel layouts, you can create additional panels to hold the component(s) you want in each cell, then add these panels to the panel that uses Box or Grid layout.
Summary of the layout managers
Layout manager | Description |
---|---|
FlowLayout | Lays out components from left to right. |
BorderLayout | Lays out components in five regions. |
CardLayout | Lays out components on a card where only one card is visible at a time. Rather than using this class, you can use the JTabbedPane class. |
BoxLayout | Lays out components in a horizontal or vertical row of cells. |
GridLayout | Lays out components in a rectangular grid of cells where each cell is the same size. |
GridBagLayout | Lays out components horizontally and vertically in a rectangular grid. |
CardLayout
CardLayout places components in the container as cards. Only one card is visible at a time, and the container acts as a stack of cards. The ordering of cards is determined by the container's own internal ordering of its component objects. CardLayout defines a set of methods that allow an application to flip through the cards sequentially or to show a specified card directly.
BoxLayout
Flow layout arranges components in rows. javax.swing.BoxLayout is a Swing layout manager that arranges components in a row or a column. To create a BoxLayout, use the following constructor:
public BoxlayLayout(Container target, int axis)This constructor is different from other layout constructors. The constructor creates a layout manager that is dedicated to the given target container. The axis parameter is BoxLayout.X_AXIS or BoxLayout.Y_AXIS, which specifies whether the components are laid out horizontally or vertically.
Code that creates a horizontal Box layout
JPanel p1 = new JPanel(); BoxLayout boxLayout = new BoxLayout(p1, BoxLayout.X_AXIS); p1.setLayout(boxLayout); You still need to invoke the setLayout method on p1 to set the layout manager.
Code that creates a Box layout panel
class BoxLayoutPanel extends JPanel
{
public BoxLayoutPanel()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JLabel("One"));
add(new JTextField("Two"));
add(new JLabel("Three"));
add(new JTextField("Four"));
add(new JCheckBox("Five"));
add(new JButton("Six"));
add(new JButton("Seven"));
add(new JButton("Eight"));
}
}
Code that creates a Grid layout manager
setLayout(new GridLayout(4, 2));