[JavaSwing] BoxLayout in Java

As you know the Container (as JFrame, JPanel, ...) used to store the control of its, however they may or may not default arrangement of objects is not our desired. So we need to use to do this Layout. In other words the Layout help we can arrange the containers in a reasonable manner and beautiful.

Content
Create and set BoxLayout for Container

Boxlayout a Layout allows us to sort of control on a horizontal or on a vertical column.
When we arranged horizontally, the other with FlowLayout, the control will not automatically down the line if not enough room for them.

For example, to create and use BoxLayout

We will have a range of JButton, including 1 Change JButton that will allow us to switch the display from horizontal to vertical and vice versa.

package nguyenvanquan7826.BoxLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyBoxLayout extends JFrame {
	private JPanel mainPanel;
	private BoxLayout boxLayout;
	private boolean axis = true;

	public MyBoxLayout() {
		createJFrame();
	}

	private void createJFrame() {
		// create JFrame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 200);
		// add content
		mainPanel = createMainPanel();
		add(mainPanel);
		// display
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private JPanel createMainPanel() {
		// create panel with BoxLayout
		JPanel panel = new JPanel();
		boxLayout = new BoxLayout(panel, BoxLayout.X_AXIS);
		panel.setLayout(boxLayout);
		// add button change to main panel
		JButton btnChange = new JButton("Change");
		btnChange.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				change();
			}
		});
		panel.add(btnChange);
		// add list button to main panel
		for (int i = 0; i < 5; i++) {
			panel.add(new JButton("Button" + i));
		}
		return panel;
	}

	// change axis when click button change
	private void change() {
		int ax = axis ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS;
		// reset boxLayout
		boxLayout = new BoxLayout(mainPanel, ax);
		// set boxLayout to mainPanel
		boxLayout.layoutContainer(mainPanel);
		axis = !axis;
	}

	public static void main(String[] args) {
		new MyBoxLayout();
	}
}

use BoxLayout
Have you noticed in the previous section can we just created JPanel, Layout has set for it to be right in the form:
[JPanel panel = new JPanel(new LayoutManager());
But with BoxLayout we can not do so because of the BoxLayout constructor is there for our Container.
BoxLayout(Container target, int axis): Create BoxLayout layout is axis.
So we need to initialize Container (JPanel) before, then initialize BoxLayout and finally use the function setLayout Layout to set the JPanel.
The arrangement of objects vertically or horizontally depending on the argument axis, X_AXIS be arranged horizontally, Y_AXIS is vertically.
Updating…