The BoxLayout class is used to arrange the components either vertically (along Y-axis) or horizontally (along X-axis). In BoxLayout class, the components are put either in a single row or a single column. The components will not wrap so, for example, a horizontal arrangement of components will stay horizontally arranged when the frame is resized.
Constructor of the class:
- BoxLayout(Container c, int axis): Creates a BoxLayout class that arranges the components with the X-axis or Y-axis.
Commonly Used Methods:
- addLayoutComponent(Component cmp, Object obj): It is not used by this class.
- getLayoutAlignmentX(Container con): Returns the alignment along the X axis for the container.
- getLayoutAlignmentY(Container con): Returns the alignment along the Y axis for the container.
- maximumLayoutSize(Container con): Returns the maximum dimensions the target container can use to lay out the components it contains.
- minimumLayoutSize(Container con): Returns the minimum dimensions needed to lay out the components contained in the specified target container.
- removeLayoutComponent(Component cmp): It is not used by this class.
- layoutContainer(Container tar): Called by the AWT when the specified container needs to be laid out.
Below programs illustrate the BoxLayout class:
- Program 1: In the below program we are arranging several JLabel components in a JFrame. We create 1 JPanel component named “Panel” and 5 JButton components named “jbtn1“, “jbtn2“, “jbtn3“, “jbtn4“, “jbtn5“. Also, we are creating a BoxLayout component named “boxlayout” and one JFrame class and then add them to the JFrame by using add() method. We set the visibility of the frame using setvisible() method. The layout is set using setLayout() method.
// Java program to demonstrate BoxLayout// class along X-Axisimportjavax.swing.JFrame;importjavax.swing.JButton;importjavax.swing.BoxLayout;importjavax.swing.Box;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importjava.awt.Insets;importjava.awt.Dimension;// taking a class DemopublicclassDemo {// Main Methodpublicstaticvoidmain(String[] args){// Function to set up the window frame.JFrame.setDefaultLookAndFeelDecorated(true);// Creating Object of "JFrame" classJFrame frame =newJFrame("BoxLayout Example X_AXIS");// Declaration of objects of JButton class.JButton jbtn1, jbtn2, jbtn3, jbtn4, jbtn5;// Function to set the default// close operation of JFrame the.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set the panel to add buttonsJPanel panel =newJPanel();// Creating Object of "boxlayout" in// X_Axis from left to rightBoxLayout boxlayout =newBoxLayout(panel, BoxLayout.X_AXIS);// to set the box layoutpanel.setLayout(boxlayout);// Set border for the panelpanel.setBorder(newEmptyBorder(newInsets(100,150,100,150)));// Initialization of object "jb1" of JButton class.jbtn1 =newJButton("Button 1");// Initialization of object "jb2" of JButton class.jbtn2 =newJButton("Button 2");// Initialization of object "jb3" of JButton class.jbtn3 =newJButton("Button 3");// Initialization of object "jb4" of JButton class.jbtn4 =newJButton("Button 4");// Initialization of object "jb5" of JButton class.jbtn5 =newJButton("Button 5");// Adding JButton "jb1" on JFramepanel.add(jbtn1);// Adding JButton "jb2" on JFramepanel.add(jbtn2);// Adding JButton "jb3" on JFramepanel.add(jbtn3);// Adding JButton "jb4" on JFramepanel.add(jbtn4);// Adding JButton "jb5" on JFramepanel.add(jbtn5);// Function to set the panel of JFrame.frame.add(panel);// Function to use the pack of JFrame.frame.pack();// Function to set visible status of JFrame.frame.setVisible(true);}}Output:
- Program 2: The following program arranges the components along Y-AXIS in a JFrame. We create 1 JPanel components named “Panel”, 5 JButton components named “jbtn1“, “jbtn2“, “jbtn3“, “jbtn4“, “jbtn5“, a BoxLayout components named “boxlayout” and a one JFrame class and then add them to the JFrame by using add() method. We will set the visibility of the frame by using setvisible() method. The layout is set by using setLayout() method.
// Java program to demonstrate BoxLayout// class along Y-Axisimportjavax.swing.JFrame;importjavax.swing.JButton;importjavax.swing.BoxLayout;importjavax.swing.Box;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importjava.awt.Insets;importjava.awt.Dimension;// construct a class Demo_1publicclassDemo_1 {// Main Methodpublicstaticvoidmain(String[] args){// Function to set up the window frame.JFrame.setDefaultLookAndFeelDecorated(true);// Creating Object of "JFrame" classJFrame frame =newJFrame("BoxLayout Example Y_AXIS");// Declaration of objects of JButton class.JButton jbtn1, jbtn2, jbtn3, jbtn4, jbtn5;// Function to set the default close operation of JFrame the.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set the panel to add buttonsJPanel panel =newJPanel();// Creating Object of "boxlayout" in Y_Axis from top to downBoxLayout boxlayout =newBoxLayout(panel, BoxLayout.Y_AXIS);// to set the box layoutpanel.setLayout(boxlayout);// Set border for the panelpanel.setBorder(newEmptyBorder(newInsets(100,150,100,150)));// Initialization of object "jb1" of JButton class.jbtn1 =newJButton("Button 1");// Initialization of object "jb2" of JButton class.jbtn2 =newJButton("Button 2");// Initialization of object "jb3" of JButton class.jbtn3 =newJButton("Button 3");// Initialization of object "jb4" of JButton class.jbtn4 =newJButton("Button 4");// Initialization of object "jb5" of JButton class.jbtn5 =newJButton("Button 5");// Adding JButton "jb1" on JFramepanel.add(jbtn1);// Adding JButton "jb2" on JFramepanel.add(jbtn2);// Adding JButton "jb3" on JFramepanel.add(jbtn3);// Adding JButton "jb4" on JFramepanel.add(jbtn4);// Adding JButton "jb5" on JFramepanel.add(jbtn5);// Function to set the panel of JFrame.frame.add(panel);// Function to use the pack of JFrame.frame.pack();// Function to set visible status of JFrame.frame.setVisible(true);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/BoxLayout.html
