BoxLayout
BoxLayout as Layout Manager in Java
Details: BoxLayout
BoxLayout is a layout manager in Java that uses a box model to arrange user interface (UI) elements. The name of the class implementing BoxLayout is BoxLayout. The BoxLayout class can be used to implement a horizontal or vertical container where each element has a specific position or size. In other words, BoxLayout organizes content in boxes that are positioned and sized as needed. BoxLayout is designed to let developers quickly implement layouts suitable for various devices, such as tablets, smartphones and televisions.
Guide: BoxLayout
In this post, we will explore BoxBorderLayout as Layout Manager in Java. In other to achieve this, the following steps can be adhered to:
1 – Create a Java program with the file name, boxorderLayout.java in JCreator or NetBeans.
2 – Proceed to import javax.swing.* package library, used to access the BoxLayout, JButton, and JFrame class.
import javax.swing.*; // used to access the BoxLayout, JButton, and JFrame class
3 – Now, initiate the variable in your main;
– variable frame for JFrame
– Insert 3-buttons; b1, b2, and b3 as JButton.
JFrame frame = new JFrame("BoxLayout Component");
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
We will present a BoxLayout class and will name it variable boxLayout. The BoxLayout consequently organizes your components in line or column position or the x and y axis position properties accompanied by its size. The BoxLayout has one constructor known as the Target Components and the Axis Alignment Property.
The Target argument is where the components are to be set and the Axis argument shows what setting the components should have. There are 4 axis constants:
– BoxLayout.X_AXIS,
– BoxLayout.Y_AXIS,
– BoxLayout.LINE_AXIS, and
– BoxLayout.PAGE_AXIS.
In other to present a BoxLayout, use the code underneath:
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS);
4 – Proceed to use the add method to add the buttons in the frame.
frame.getContentPane().add(b1);
frame.getContentPane().add(b2);
frame.getContentPane().add(b3);
Utilize the setLayout method of the frame to insert a BoxLayout as the layout manager.
frame.getContentPane().setLayout(boxLayout);
5 – Finally, we can conclude by packing the frame, set visibility and the close operation of the frame using the code below:
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Output: BoxLayout
Complete Source Code
import javax.swing.*; // used to access the BoxLayout, JButton, and JFrame class
import javax.swing.JButton;
import javax.swing.JFrame;
public class boxLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout Component");
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS);
frame.getContentPane().setLayout(boxLayout);
frame.getContentPane().add(b1);
frame.getContentPane().add(b2);
frame.getContentPane().add(b3);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}