Introduction
A lightweight container that manages its layout using a BoxLayout object. Several class methods offered by Box are beneficial for containers using BoxLayout. Glue, struts, and rigid areas are a few different types of invisible elements that the Box class is capable of producing.
If all the components the Box contains have a fixed size, the positions of the components could be controlled by using glue components. If a fixed amount of space is required between components, then the strut could be used.
This class is an oddity in the JFC. It provides a lightweight container that is configured with the BoxLayout layout manager. When it is configured to align components along the y-axis, adding components to the Box is like stacking objects in a box. The BoxLayout class is a new layout manager. The Box class also contains several static methods that create invisible components to help control the layout of the container. It does not extend the JComponent, and hence the properties of this class are not inherited.
BoxLayout Class
A layout manager called BoxLayout Class enables the vertical or horizontal arrangement of multiple components. The components won't wrap, so when the frame is resized, for instance, a vertical arrangement of components will stay vertical. This is a layout manager contained in the JFC. It is similar to the FlowLayout layout manager.
It allows the components to be stacked vertically as well as placed horizontally. The layout manager also does not wrap components. The orientation of the BoxLayout is specified during construction and cannot be altered thereafter. The constant X_AXIS and Y_AXIS are defined in the BoxLayout class to specify a left-to-right or top-to-bottom component arrangement.
The BoxLayout class represents a component's minimum and maximum size properties. When components are stacked along the Y-axis, the BoxLayout class tries to size each component to its preferred height. Each component is of the same width. The BoxLayout class adheres to the minimum and maximum sizes of components while laying out a container.
Invisible and rigid Components
Since components are placed next to each other, the container appears cramped. This could be avoided by adding invisible components to the container. The Box class contains static methods that return invisible components, which could be used to arrange components in the container/ There are three types of invisible components, these include rigid, glue, and strut.
Rigid Components take up a defined amount of space and are not resizable. Glue components will stretch to any size required by the layout manager.
The source code illustrates the usage of the BoxLayout layout manager.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class ashBox extends JFrame {
public ashBox() {
setTitle("AB Box");
JPanel contentpane = (JPanel) getContentPane();
contentpane.setLayout(new BorderLayout());
Box mainbox = new Box(BoxLayout.Y_AXIS);
JButton ok = new JButton("OK");
contentpane.add("North", ok);
JButton cancel = new JButton("CANCEL");
contentpane.add("South", cancel);
myadapter myapp = new myadapter();
addWindowListener(myapp);
}
private class myadapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String args[]) {
ashBox b = new ashBox();
b.setSize(400, 400);
b.setVisible(true);
}
}
Output
Summary
Multiple components can be arranged vertically or horizontally using the BoxLayout manager. It is comparable to the layout manager for flowlayout. Components are not wrapped.