[JavaSwing] GridLayout

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
Created using GridLayout

GridLayout is a Layout Simple and easy to use, it divided our container into table consists of rows and columns of equal size.
We use GridLayout layout when we like a table, such as the layout of JButton in the computer like this.
GridLayout

For example, using GridLayout

In use for this we will create 1 table includes 4 JButton placed in a JPanel Using GridLayout. When you click on each JButton, you can change the horizontal distance, along between them. Below 2 JLabel display horizontal distance (hGap), along (vGap) between them.

package nguyenvanquan7826.GridLayout;

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

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

public class MyGridLayout extends JFrame implements ActionListener {

	private JPanel mainPanel;
	private GridLayout gridLayout;
	private JLabel lbHGap;
	private JLabel lbVGap;
	final int dram = 3;

	public MyGridLayout() {
		createJFrame();
	}

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

	private JPanel createMainPanel() {
		// create JPanel with GridLayout
		gridLayout = new GridLayout(3, 2, 5, 5);
		JPanel panel = new JPanel(gridLayout);
		// add four JButton to panel
		panel.add(createJButton("+ vGap"));
		panel.add(createJButton("- vGap"));
		panel.add(createJButton("+ hGap"));
		panel.add(createJButton("- hGap"));
		// create and add vGap and hGap JLabel to panel
		lbVGap = createJLabel("vGap = 5");
		lbHGap = createJLabel("hGap = 5");
		panel.add(lbVGap);
		panel.add(lbHGap);
		return panel;
	}

	// create JButton
	private JButton createJButton(String buttonName) {
		JButton btn = new JButton(buttonName);
		btn.addActionListener(this);
		return btn;
	}

	// create JLabel
	private JLabel createJLabel(String title) {
		JLabel lb = new JLabel(title);
		lb.setHorizontalAlignment(JLabel.CENTER);
		return lb;
	}

	// change vGap and hGap
	private void changeGap(int vGap, int hGap) {
		if (vGap >= 0 && hGap >= 0) {
			gridLayout.setVgap(vGap);
			gridLayout.setHgap(hGap);
			lbVGap.setText("vGap = " + vGap);
			lbHGap.setText("hGap = " + hGap);
			gridLayout.layoutContainer(mainPanel);
		}
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		String command = evt.getActionCommand();
		if (command == "+ vGap") { // augment vGap
			changeGap(gridLayout.getVgap() + dram, gridLayout.getHgap());
		}
		if (command == "- vGap") {// abatement vGap
			changeGap(gridLayout.getVgap() - dram, gridLayout.getHgap());
		}
		if (command == "+ hGap") {// augment hGap
			changeGap(gridLayout.getVgap(), gridLayout.getHgap() + dram);
		}
		if (command == "- hGap") {// abatement hGap
			changeGap(gridLayout.getVgap(), gridLayout.getHgap() - dram);
		}
	}

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

use GridLayout
The initialization method GridLayout
– GridLayout(): Create GridLayout with 1 row, 1 post
– GridLayout(int rows, int cols): Create GridLayou the number of rows and columns specified
– GridLayout(int rows, int cols, int hgap, int vgap): Create GridLayout with customers, number of columns, the distance between the rows and columns.

In this code you command attention gridLayout.layoutContainer(main panel);, This command allows Container (This is in MainPanel) use this layout (here is gridLayout).

Refer to: class GridLayout, use GridLayout