[JavaSwing] JPanel

Content
Create JPanel
Set Layout and Border

After the opening introduction JFrame and the introduction of JLabel, JButton and JTextField maybe you have become familiar with how to create an interface in Java Swing. Prior to the introduction of frequently used objects, I think we should learn a very important it is to Container (container, holder) and placing Layout (The layout) in the container it.

JPanel
JPanel is a container (container) it used to contain similar objects JFrame but it is not 1 JFrame. Easier to understand, you can imagine our house 1 JFrame, remaining bedrooms, living room, dining room which is the JPanel, ie in a JFrame contains the JPanel, in each JPanel may contain objects or even the other JPanel.

Create JPanel

We have 2 The constructor or use JPanel which is:
– JPanel(): Create 1 The default layout is JPanel with FlowLayout.
– JPanel(LayoutManager layout): Create 1 JPanel with the specified layout.

Example 1: Create 1 JFrame containing 2 JPanel, 1 JPanel containing JLabel, 1 JPanel containing 2 JButton allows adding or removing JLabel.

package nguyenvanquan7826.JPanel;

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 CreateJPanel extends JFrame implements ActionListener {
	private int numlabel = 0; // number JLabel in panel
	JPanel panel1, panel2;

	public CreateJPanel() {
		createJFrame();
	}

	// create JFrame
	private void createJFrame() {
		setTitle("Create JPanel");
		setLayout(new GridLayout(2, 1, 5, 5));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		createContent();

		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private void createContent() {
		// create panel1 with layout default is FlowLayout
		panel1 = new JPanel();
		// add a label into panel1
		addJLabel(panel1);

		// create panel2 with GridLayout
		panel2 = new JPanel(new GridLayout(1, 2, 5, 5));
		panel2.add(createJButton("Add a JLabel"));
		panel2.add(createJButton("Remove all JLabel"));

		// add panel1 and panel2 into JFrame
		add(panel1);
		add(panel2);
	}

	// add JLabel to panel and repaint
	private void addJLabel(JPanel panel) {
		panel.add(new JLabel("JLabel" + (++numlabel)));
		panel.validate();
		panel.repaint();
	}

	// remove all JLabel and repaint JPanel
	private void removeAllJLabel(JPanel panel) {
		panel.removeAll();
		panel.validate();
		panel.repaint();
		numlabel = 0;
	}

	// create a 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();
		if (command == "Add a JLabel") {
			addJLabel(panel1);
			System.out.println("add" + panel1.getComponentCount());
		}
		if (command == "Remove all JLabel") {
			removeAllJLabel(panel1);
		}
	}

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

create JPanel
In this example we create 2 JPanel, Panel1 is initialized by default, no parameters passed to it with the default layout is FlowLayout (the layout of objects in sequence). panel2 is initialized with the specified layout is GridLayout (layout of objects in each cell are equal).
You also pay attention to 2 jaw addJLabel(JPanel panel) and removeAllJLabel(JPanel panel), after using the add method, or removeAll of JPanel, want to change it (More JLabel, or delete entire JLabel) shown, Updates on JFrame, we need to call 2 validate method() and repaint() to render objects that can JPanel. We can also do the same with JFrame.

Set Layout and Border for JPanel

To place Layout and Border the methods we use JPanel setLayout and setBorder.

Refer to: class JPanel, Use Panel