[JavaSwing] FlowLayout

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 FlowLayout for Container

Note you that from now on all code can be long, so my only comment sections related to the topic of the article or section has not yet. With no comment other part that is mentioned in the previous post and, You can find the blog by typing keywords into the search box.

FlowLayout an arrangement of objects on a line, from left to right. We use a FlowLayout if you want to sort the objects sequentially on a line.

Create and set FlowLayout for Container

The following example will create and put FlowLayout for JPanel contains JButton Figure:
FlowLayout

package nguyenvanquan7826.FlowLayout;

import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class MyFlowLayout extends JFrame implements ActionListener {

	private String buttonName[] = { "FlowLayout", "add", "controls", "in",
			"the", "line" };

	public MyFlowLayout() {
		createJFrame();
	}

	// create and display JFrame
	private void createJFrame() {
		setTitle("My FlowLayout");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(550, 200);

		// add panel content to JFrame
		add(createJPanelContent());

		setLocationRelativeTo(null);
		setVisible(true);
	}

	// add content in JFrame
	private JPanel createJPanelContent() {
		// create FlowLayout
		FlowLayout layout = new FlowLayout(FlowLayout.RIGHT, 10, 20);
		// create a JPanel container control (JButton)
		JPanel panel = new JPanel(layout);
		// add array JButton to panel
		for (int i = 0; i < buttonName.length; i++) {
			panel.add(createJButton(buttonName[i]));
		}
		return panel;
	}

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

	@Override
	public void actionPerformed(ActionEvent evt) {
		String command = evt.getActionCommand();
		for (int i = 0; i < buttonName.length; i++) {
			if (command == buttonName[i]) {
				System.out.println(buttonName[i]);
			}
		}
	}

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


The constructor FlowLayout include:

– FlowLayout(): Initialize FlowLayout object with default alignment is CENTER (centered) and the distance between the objects in the horizontal and vertical default 5 unit.
– FlowLayout(int align): Same as above but we only locate alignment as: CENTER, LEFT, RIGHT, LEADING, TRAILING (LEADING, TRAILIN and indicate radical left, according to the relative arrangement of the Container, refer in The difference between LEFT and LEADING, RIGHT và TRAILING).
– FlowLayout(int align, int hgap, int vgap): With this we will initiate self-aligned and spaced horizontal distance (hgap), along (vgap) between objects.
FlowLayout
Because the default JPanel using FlowLayout default (aligns CENTER, distance hgap = vgap = 5) so I created 1 The new layout left flat (RIGHT), distance between objects horizontally 10, along the 20. When you resize the width of the JFrame do not contain enough JButton on, they will automatically be pushed to the bottom line as shown below.

Refer to: class FlowLayout, Use FlowLaout