Java AWT | BoxLayout Class

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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate BoxLayout 
    // class along X-Axis
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import javax.swing.Box;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Insets;
    import java.awt.Dimension;
      
    // taking a class Demo
    public class Demo {
      
        // Main Method
        public static void main(String[] args)
        {
      
            // Function to set up the window frame.
            JFrame.setDefaultLookAndFeelDecorated(true);
      
            // Creating Object of "JFrame" class
            JFrame frame = new JFrame("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 buttons
            JPanel panel = new JPanel();
      
            // Creating Object of "boxlayout" in 
            // X_Axis from left to right
            BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.X_AXIS);
      
            // to set the box layout
            panel.setLayout(boxlayout);
      
            // Set border for the panel
            panel.setBorder(new EmptyBorder(new Insets(100, 150, 100, 150)));
      
            // Initialization of object "jb1" of JButton class.
            jbtn1 = new JButton("Button 1");
      
            // Initialization of object "jb2" of JButton class.
            jbtn2 = new JButton("Button 2");
      
            // Initialization of object "jb3" of JButton class.
            jbtn3 = new JButton("Button 3");
      
            // Initialization of object "jb4" of JButton class.
            jbtn4 = new JButton("Button 4");
      
            // Initialization of object "jb5" of JButton class.
            jbtn5 = new JButton("Button 5");
      
            // Adding JButton "jb1" on JFrame
            panel.add(jbtn1);
      
            // Adding JButton "jb2" on JFrame
            panel.add(jbtn2);
      
            // Adding JButton "jb3" on JFrame
            panel.add(jbtn3);
      
            // Adding JButton "jb4" on JFrame
            panel.add(jbtn4);
      
            // Adding JButton "jb5" on JFrame
            panel.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);
        }
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate BoxLayout 
    // class along Y-Axis
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import javax.swing.Box;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Insets;
    import java.awt.Dimension;
      
    // construct a class Demo_1
    public class Demo_1 {
      
        // Main Method
        public static void main(String[] args)
        {
      
            // Function to set up the window frame.
            JFrame.setDefaultLookAndFeelDecorated(true);
      
            // Creating Object of "JFrame" class
            JFrame frame = new JFrame("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 buttons
            JPanel panel = new JPanel();
      
            // Creating Object of "boxlayout" in Y_Axis from top to down
            BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
      
            // to set the box layout
            panel.setLayout(boxlayout);
      
            // Set border for the panel
            panel.setBorder(new EmptyBorder(new Insets(100, 150, 100, 150)));
      
            // Initialization of object "jb1" of JButton class.
            jbtn1 = new JButton("Button 1");
      
            // Initialization of object "jb2" of JButton class.
            jbtn2 = new JButton("Button 2");
      
            // Initialization of object "jb3" of JButton class.
            jbtn3 = new JButton("Button 3");
      
            // Initialization of object "jb4" of JButton class.
            jbtn4 = new JButton("Button 4");
      
            // Initialization of object "jb5" of JButton class.
            jbtn5 = new JButton("Button 5");
      
            // Adding JButton "jb1" on JFrame
            panel.add(jbtn1);
      
            // Adding JButton "jb2" on JFrame
            panel.add(jbtn2);
      
            // Adding JButton "jb3" on JFrame
            panel.add(jbtn3);
      
            // Adding JButton "jb4" on JFrame
            panel.add(jbtn4);
      
            // Adding JButton "jb5" on JFrame
            panel.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);
        }
    }

    chevron_right

    
    

    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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.