Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Creating a JCheckBox in Java Swings

In this lesson, you will learn.

  • Working with JCheckBox
  • Constructors and Methods of JCheckBox
  • Examples

 

What is a JCheckBox

 

 

  1. JCheckbox is a GUI component that can be either checked or unchecked.
  2. It inherits from the JToggleButton class, which is a subclass of AbstractButton.

 

Constructors of JCheckBox

Constructor Description
JCheckBox() Creates an initially unselected checkbox button with no text.
JCheckBox(String text) Creates an initially unselected checkbox with the specified text.
JCheckBox(String text, boolean selected) Creates a checkbox with text and specifies whether it is selected.
JCheckBox(Icon icon) Creates an initially unselected checkbox with an icon.
JCheckBox(Icon icon, boolean selected) Creates a checkbox with an icon and specifies whether it is selected.
JCheckBox(String text, Icon icon) Creates a checkbox with text and icon.
JCheckBox(String text, Icon icon, boolean selected) Creates a checkbox with text, icon, and specifies whether it is selected.

 

Creating a JCheckBox

 

Example -1: Creating a JCheckBox on JFrame

package jcheckbox;

import javax.swing.*;
import java.awt.*;

public class JCheckBoxExample extends JFrame {

	public JCheckBoxExample() {
		// Set the frame attributes
		setTitle("JCheckBox Example");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 300);
		setLayout(new FlowLayout());//Align component from left to right

		// Create JCheckBox using different constructors
		JCheckBox checkBox1 = new JCheckBox();
		checkBox1.setText("Digital Art");
		//checkBox1.setBounds(50, 20, 100, 30);
		checkBox1.setSelected(true);
		checkBox1.setToolTipText("This is Digital Art");

		JCheckBox checkBox2 = new JCheckBox("Dance");
		JCheckBox checkBox3 = new JCheckBox("Writing", true);
		JCheckBox checkBox4 = new JCheckBox("Photography");

		// Add the checkboxes to the frame
		add(checkBox1);
		add(checkBox2);
		add(checkBox3);
		add(checkBox4);
		
		// Set frame to visible
		setVisible(true);
	}

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

 

Output

 

 

Summary of Common Methods in JCheckBox

Method Description
boolean isSelected() Returns the state of the checkbox, true if selected, false otherwise.
void setSelected(boolean b) Sets the state of the checkbox.
Icon getIcon() Returns the icon used by the checkbox.
void setIcon(Icon icon) Sets the icon for the checkbox.
String getText() Returns the text displayed on the checkbox.
void setText(String text) Sets the text of the checkbox.
Icon getSelectedIcon() Returns the icon used when the checkbox is selected.
void setSelectedIcon(Icon selectedIcon) Sets the icon to be displayed when the checkbox is selected.
void setEnabled(boolean b) Enables or disables the checkbox.
boolean isEnabled() Returns whether the checkbox is enabled.
Object[] getSelectedObjects() Returns an array of all the selected items (typically only one item for a checkbox).

 

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review