Java Swing | JCheckBox with examples - GeeksforGeeks

23 May, 2018
JCheckBox is a part of Java Swing package . JCheckBox can be selected or deselected . It displays it state to the user . JCheckBox is an implementation to checkbox . JCheckBox inherits JToggleButton class.
Constructor of the class are :
JCheckBox() : creates a new checkbox with no text or iconJCheckBox(Icon i) : creates a new checkbox with the icon specifiedJCheckBox(Icon icon, boolean s) : creates a new checkbox with the icon specified and the boolean value specifies whether it is selected or not.JCheckBox(String t) :creates a new checkbox with the string specifiedJCheckBox(String text, boolean selected) :creates a new checkbox with the string specified and the boolean value specifies whether it is selected or not.JCheckBox(String text, Icon icon) :creates a new checkbox with the string and the icon specified.JCheckBox(String text, Icon icon, boolean selected): creates a new checkbox with the string and the icon specified and the boolean value specifies whether it is selected or not.
JCheckBox() : creates a new checkbox with no text or icon
JCheckBox(Icon i) : creates a new checkbox with the icon specified
JCheckBox(Icon icon, boolean s) : creates a new checkbox with the icon specified and the boolean value specifies whether it is selected or not.
JCheckBox(String t) :creates a new checkbox with the string specified
JCheckBox(String text, boolean selected) :creates a new checkbox with the string specified and the boolean value specifies whether it is selected or not.
JCheckBox(String text, Icon icon) :creates a new checkbox with the string and the icon specified.
JCheckBox(String text, Icon icon, boolean selected): creates a new checkbox with the string and the icon specified and the boolean value specifies whether it is selected or not.
Methods to add Item Listener to checkbox.
addActionListener(ItemListener l): adds item listener to the componentitemStateChanged(ItemEvent e) : abstract function invoked when the state of the item to which listener is applied changesgetItem() : Returns the component-specific object associated with the item whose state changedgetStateChange() : Returns the new state of the item. The ItemEvent class defines two states: SELECTED and DESELECTED.getSource() : Returns the component that fired the item event.
addActionListener(ItemListener l): adds item listener to the component
itemStateChanged(ItemEvent e) : abstract function invoked when the state of the item to which listener is applied changes
getItem() : Returns the component-specific object associated with the item whose state changed
getStateChange() : Returns the new state of the item. The ItemEvent class defines two states: SELECTED and DESELECTED.
getSource() : Returns the component that fired the item event.
Commonly used methods:
setIcon(Icon i) : sets the icon of the checkbox to the given iconsetText(String s) :sets the text of the checkbox to the given textsetSelected(boolean b) : sets the checkbox to selected if boolean value passed is true or vice versagetIcon() : returns the image of the checkboxgetText() : returns the text of the checkboxupdateUI() : resets the UI property with a value from the current look and feel.getUI() : returns the look and feel object that renders this component.paramString() : returns a string representation of this JCheckBox.getUIClassID() : returns the name of the Look and feel class that renders this component.getAccessibleContext() : gets the AccessibleContext associated with this JCheckBox.isBorderPaintedFlat() : gets the value of the borderPaintedFlat property.setBorderPaintedFlat(boolean b) : sets the borderPaintedFlat property,
setIcon(Icon i) : sets the icon of the checkbox to the given icon
setText(String s) :sets the text of the checkbox to the given text
setSelected(boolean b) : sets the checkbox to selected if boolean value passed is true or vice versa
getIcon() : returns the image of the checkbox
getText() : returns the text of the checkbox
updateUI() : resets the UI property with a value from the current look and feel.
getUI() : returns the look and feel object that renders this component.
paramString() : returns a string representation of this JCheckBox.
getUIClassID() : returns the name of the Look and feel class that renders this component.
getAccessibleContext() : gets the AccessibleContext associated with this JCheckBox.
isBorderPaintedFlat() : gets the value of the borderPaintedFlat property.
setBorderPaintedFlat(boolean b) : sets the borderPaintedFlat property,
The following programs will illustrate the use of JCheckBox
1. Program to create a simple checkbox using JCheckBox
// java Program to create a simple checkbox using JCheckBoximport java.awt.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame {      // frame    static JFrame f;      // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");          // set layout of frame        f.setLayout(new FlowLayout());          // create checkbox        JCheckBox c1 = new JCheckBox("checkbox 1");        JCheckBox c2 = new JCheckBox("checkbox 2");          // create a new panel        JPanel p = new JPanel();          // add checkbox to panel        p.add(c1);        p.add(c2);          // add panel to frame        f.add(p);          // set the size of frame        f.setSize(300, 300);          f.show();    }}
Output :
2. Program to create a checkbox with a icon .
// java Program to create a checkbox with a icon .import java.awt.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame {      // frame    static JFrame f;      // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");          // set layout of frame        f.setLayout(new FlowLayout());          // create checkbox        JCheckBox c1 = new JCheckBox("checkbox with image", new ImageIcon("f:/gfg.jpg"), true);        JCheckBox c2 = new JCheckBox("checkbox 2");          // create a new panel        JPanel p = new JPanel();          // add checkbox to panel        p.add(c1);        p.add(c2);          // add panel to frame        f.add(p);          // set the size of frame        f.setSize(300, 300);          f.show();    }}
Output :
3. Program to create a checkbox and ItemListener to it.
// java Program to create a checkbox and ItemListener to it.import java.awt.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame implements ItemListener {      // frame    static JFrame f;      // label    static JLabel l, l1;      // checkbox    static JCheckBox c1, c2;      // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");          // create a object        solve s = new solve();          // set layout of frame        f.setLayout(new FlowLayout());          // create checkbox        c1 = new JCheckBox("geeksforgeeks", new ImageIcon("f:/gfg.jpg"), false);        c2 = new JCheckBox("checkbox 2", false);          // add ItemListener        c1.addItemListener(s);        c2.addItemListener(s);          // create labels        l = new JLabel("geeksforgeeks not selected");        l1 = new JLabel("checkbox2 not selected");          // set color of text        l.setForeground(Color.red);        l1.setForeground(Color.blue);          // create a new panel        JPanel p = new JPanel();          // add checkbox to panel        p.add(c1);        p.add(c2);        p.add(l);        p.add(l1);          // add panel to frame        f.add(p);          // set the size of frame        f.setSize(600, 300);          f.show();    }    public void itemStateChanged(ItemEvent e)    {        // if the state of checkbox1 is changed        if (e.getSource() == c1) {            if (e.getStateChange() == 1)                l.setText("geeksforgeeks  selected");            else                l.setText("geeksforgeeks  not selected");        }          // if the state of checkbox2 is changed        else {            if (e.getStateChange() == 1)                l1.setText("checkbox 2  selected");            else                l1.setText("checkbox 2 not selected");        }    }}
Output :Note : the above programs might not run in a online compiler please use an offline IDE
java-swing
Java
Programming Language
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Interfaces in Java
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
Modulo Operator (%) in C/C++ with Examples
Differences between Procedural and Object Oriented Programming
Arrow operator -> in C/C++ with Examples
Structures in C++
Top 10 Programming Languages to Learn in 2022