BoxLayout as Layout Manager in Java

May 27, 2020
Tutorials
boxlayout in java

This instructional exercise is about the BoxLayout as Layout Manager in Java. A BoxLayout as a design originates from the Box class where it gives every component’s x and y alignment properties with its sizes.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of boxLayout.java.

2. Import the javax.swing.* package library that is used to access the BoxLayout, JButton, and JFrame class.

import javax.swing.*; // used to access the BoxLayout, JButton, and JFrame class

3. Initialize your variable in your Main, variable frame for JFrame, and 3 buttons namely 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");

 

Presently, we will introduce a BoxLayout class and will name it as variable boxLayout. The BoxLayout automatically arranges your components in line or column position, or the x and y axis position arrangement properties along with its size. The BoxLayout has one constructor known as the Target Components and the Axis Alignment Property. The Target argument is the place where the components are to be set and the Axis argument indicates what arrangement that the components must have. There are 4 axis constants: BoxLayout.X_AXIS, BoxLayout.Y_AXIS, BoxLayout.LINE_AXIS, and BoxLayout.PAGE_AXIS. Presently to announce a BoxLayout, have the code below:

BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS);

4. Then add the buttons into the frame using the add method.

frame.getContentPane().add(b1);
frame.getContentPane().add(b2);
frame.getContentPane().add(b3);

To have a BoxLayout as the layout manager, we will use the setLayout method of the frame.

frame.getContentPane().setLayout(boxLayout);

5. Lastly, pack the frame, set visibility, and the close operation of the frame. Have this code below:

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

Output:

output


Here’s the full code of this tutorial:

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);
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.