[Java Swing] JCheckBox in Java – JCheckBox in Java

Content
Create JCheckBox
Getting events for JCheckBox

JCheckBox is subject allows us to select multiple properties. For example, when a viewer to fill with money, Pet, there is no such car. He may have both 3 or no one at all.

Create JCheckBox

We will create 1 JFrame with JCheckBox as shown below:

JcheckBox in Java

JFrame include 2 The main part is on the left side contains 3 JCheckBox, right part contains 3 JLabel respectively. First we will create JCheckBox ago.

package nguyenvanquan7826.JCheckBox;

import java.awt.BorderLayout;
import java.awt.GridLayout;

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

public class MyJCheckBox extends JFrame {

	private JCheckBox checkMoney, checkHouse, checkCar;
	private JLabel lbMoney, lbHouse, lbCar;

	public MyJCheckBox() {
		// add main panel
		add(createMainPanel());

		// set display
		setTitle("JCheckBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(200, 150);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private JPanel createMainPanel() {
		// create JPanel container three checkBox
		JPanel panelCheckBox = new JPanel(new GridLayout(4, 0, 5, 5));
		panelCheckBox.add(checkMoney = createCheckBox("Money"));
		panelCheckBox.add(checkCar = createCheckBox("Car"));
		panelCheckBox.add(checkHouse = createCheckBox("House"));

		// create JPanel Container three JLabel
		JPanel panelShow = new JPanel(new GridLayout(4, 0, 5, 5));
		panelShow.add(lbMoney = createLabel("Money"));
		panelShow.add(lbCar = createLabel("Car"));
		panelShow.add(lbHouse = createLabel("House"));

		// add panelCheckBox and panelShow to main panel
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(panelCheckBox, BorderLayout.WEST);
		panel.add(panelShow, BorderLayout.CENTER);
		return panel;
	}

	// create a JCheckBox
	private JCheckBox createCheckBox(String name) {
		JCheckBox checkBox = new JCheckBox(name);
		return checkBox;
	}

	// create a JLabel
	private JLabel createLabel(String lb) {
		JLabel label = new JLabel(lb);
		label.setEnabled(false);
		return label;
	}

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

On his part interfaced not speak again, the code is relatively annotations and. I'm just talking about the creation JCheckBox.
In the method createCheckBox(String name) I have used the method to initialize JCheckBox checkBox = new JCheckBox(name); Here's how to create 1 JCheckBox with corresponding text. Also we have other initialization method:
– JCheckBox(): Create 1 JCheckBox no text, no icon, not checked.
– JCheckBox(Action a): Create an action JCheckBox
– JCheckBox(Icon icon): Create an icon JCheckBox
– JCheckBox(Icon icon, boolean selected): Create icons and settings JCheckBox have selected or not.
– JCheckBox(String text, boolean selected): Create text and set JCheckBox have selected or not.
– JCheckBox(String text, Icon icon): Create JCheckBox with text, with icon
– JCheckBox(String text, Icon icon, boolean selected): Create JCheckBox with text, with icon, setting is selected or not.

Getting events for JCheckBox

To catch the event when we need to check the checkbox for each checkbox addItemListener. Method createCheckBox() is amended as follows:

private JCheckBox createCheckBox(String name) {
	JCheckBox checkBox = new JCheckBox(name);
	checkBox.addItemListener(this);
	return checkBox;
}

When this, Our class implements the interface to ItemListener and overriding methods public void itemStateChanged(ItemEvent and). This method is a method for us to do the job when the check in the checkbox. In this example we will check each event on a checkbox to check that. Then it will check if the checkbox is selected or deselected to set display or not display the corresponding JLabel.

JcheckBox in Java

To check if the checkbox is selected or not we use the method isSelected(). So all of our programs will be written as follows:

package nguyenvanquan7826.JCheckBox;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

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

public class MyJCheckBox extends JFrame implements ItemListener {

	private JCheckBox checkMoney, checkHouse, checkCar;
	private JLabel lbMoney, lbHouse, lbCar;

	public MyJCheckBox() {
		// add main panel
		add(createMainPanel());

		// set display
		setTitle("JCheckBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(200, 150);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private JPanel createMainPanel() {
		// create JPanel container three checkBox
		JPanel panelCheckBox = new JPanel(new GridLayout(4, 0, 5, 5));
		panelCheckBox.add(checkMoney = createCheckBox("Money"));
		panelCheckBox.add(checkCar = createCheckBox("Car"));
		panelCheckBox.add(checkHouse = createCheckBox("House"));

		// create JPanel Container three JLabel
		JPanel panelShow = new JPanel(new GridLayout(4, 0, 5, 5));
		panelShow.add(lbMoney = createLabel("Money"));
		panelShow.add(lbCar = createLabel("Car"));
		panelShow.add(lbHouse = createLabel("House"));

		// add panelCheckBox and panelShow to main panel
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(panelCheckBox, BorderLayout.WEST);
		panel.add(panelShow, BorderLayout.CENTER);
		return panel;
	}

	// create a JCheckBox
	private JCheckBox createCheckBox(String name) {
		JCheckBox checkBox = new JCheckBox(name);
		checkBox.addItemListener(this);
		return checkBox;
	}

	// create a JLabel
	private JLabel createLabel(String lb) {
		JLabel label = new JLabel(lb);
		label.setEnabled(false);
		return label;
	}

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

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == checkMoney) {
			lbMoney.setEnabled(checkMoney.isSelected());
			return;
		}
		if (e.getSource() == checkCar) {
			lbCar.setEnabled(checkCar.isSelected());
			return;
		}
		if (e.getSource() == checkHouse) {
			lbHouse.setEnabled(checkHouse.isSelected());
			return;
		}
	}
}

Read more: class JCheckBox, use JCheckBox