JCheckBox in Java Example for Beginners

This exhaustive example illustrates JCheckBox in Java Swing for beginners. This also covers all core methods of the javax.swing.JCheckBox class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JCheckBoxExample extends JFrame
JPanel p;
JCheckBox c1,c2,c3,c4,c5,c6,c7,c8;
ImageIcon i1,i2,i3,i4,i5;
Font f;
JLabel label;
ButtonGroup bg=new ButtonGroup();

    public JCheckBoxExample()
    private void createAndShowGUI()
        setTitle("JCheckBox Demo");

        // Create image icons
        i1=new ImageIcon("checkbox.png");
        i2=new ImageIcon("checkboxselected.png");
        i3=new ImageIcon("checkboxpressed.png");
        i4=new ImageIcon("checkboxrollover.png");
        i5=new ImageIcon("checkboxrolloverselected.png");
        // Create a Font
        f=new Font("Myriad Pro",Font.PLAIN,16);
        // Create JLabel
        label=new JLabel();
        // Add the label to SOUTH (down) side
        // Create JPanel p
        p=new JPanel();
        // Set the layout for JPanel
        p.setLayout(new FlowLayout());
        // First constructor
        // Takes no parameters
        c1=new JCheckBox();

        // Create an action that produces beep sound
        Action a=new javax.swing.text.DefaultEditorKit.BeepAction();
        // Second constructor
        // Takes Action object
        c2=new JCheckBox(a);
        // Third constructor
        // Takes ImageIcon
        // Icon appears instead
        // of the check/uncheck shape
        c3=new JCheckBox(i1);
        // Create JCheckBox
        // Takes ImageIcon which
        // is the default icon
        // (icon when not selected)
        // and boolean value indicating
        // selected/unselected
        c4=new JCheckBox(i1,true);
        // Fifth constructor
        // Takes a String which appears
        // on the JCheckBox
        c5=new JCheckBox("Checkbox 5");
        // Sixth constructor
        // Takes String which appears on
        // the JCheckBox, and a boolean
        // indicating selected/unselected
        c6=new JCheckBox("Checkbox 6",true);

        // Seventh constructor
        // Take a string that appears
        // on the JCheckBox with an icon
        c7=new JCheckBox("Checkbox 7",i1);
        // Eighth constructor
        // Takes a string that appears
        // on JCheckBox with an ImageIcon
        // (the default icon) and the boolean
        // indicating selected/unselected
        c8=new JCheckBox("Checkbox 8",i1,true);
        // Set some action
        // Set some text
        c1.setText("Checkbox 1");
        c2.setText("Checkbox 2");
        c3.setText("Checkbox 3");
        // Set some font
        // Set selected icon
        // Set focus painted false
        // This removes the border
        // around the text
        // Default is true
        // Set some cursor
        // The cursor is default cursor
        c7.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // Set the icon text gap
        // This gap is relative
        // to the position of the
        // text on the JCheckBox
        // i.e. if the text is aligned
        // vertically, the icon text gap
        // is vertical, if horizontal, it
        // is horizontal
        // Default is 4
        // Set vertical text
        // position
        // Set horizontal text position
        // Set pressed icon
        // This icon appears when the
        // mouse/space bar/mnemonic
        // is pressed on the JCheckBox
        // Set rollover icon
        // This icon appears when
        // the mouse is hover on the
        // JCheckBox
        // Set roll over selected icon
        // This icon appears when the
        // mouse is hover on the JCheckBox
        // when it is selected
        // Set mnemonic
        // 56 is ascii value for 8
        // c8.setMnemonic(56);
        // Alternatively, if you
        // don't know the ascii
        // Call the setMnemonic(int)
        // not setMnemonic(char)
        // To work with mnemonic
        // the user must press 8
        // with Alt pressed
        // Note: Another method called
        // setMnenomic(char) is obsolete
        // This method only handles char
        // values between a and z or A and Z
        // as per the documentation
        // Changing displayed mnemonic
        // index. This changes the underlined
        // letter
        // Set border painted
        // Adding to ButtonGroup
        // Usually JRadioButtons are
        // added to a ButtonGroup
        // Adding to a button group
        // will make only one of the
        // JCheckBox in the ButtonGroup
        // selectable, if another
        // one is selected, the selected
        // one is deselected
        // Set border painted
        // By default this is false
        // for JCheckBox
        // Set content area filled
        // It is true by default
        // If false, the background
        // cannot be seen
        // Set roll over enabled
        // Disable c4
        // Set the background
        // Set the foreground
        // Set margin
        // The method setMargin() takes
        // java.awt.Insets object which takes
        // top,left,bottom.right respectively
        // Default is 2,2,2,2
        // This sets the top,left,bottom,right
        // space between border and icon,text
        c1.setMargin(new Insets(10,10,10,10));
        // Set a custom border for c2
        // Set opaque to false
        // By default it is true
        // If not opaque, the background
        // is not visible (if painted)
        // Now set the background
        // This is not visible
        // Set auto scrolls
        // to true, the default value
        // is false.
        // When auto scrolls is true
        // mouseDragged events will be generated
        // even outside the bounds of the
        // component to which MouseMotionListener
        // is registered, provided that the mouse is
        // dragged from the point within the component's
        // bounds.
        // Set focusable false
        // This component will not
        // receive focus
        // Handle mouseDragged
        c1.addMouseMotionListener(new MouseAdapter(){
            public void mouseDragged(MouseEvent me)
                System.out.println("Mouse dragged.");
        // Add all check boxes
        // to the JPanel
        // Set tool tip text
        // Get all the components of p
        // Should be done after adding
        // JCheckBoxes to p
        // since, before they are added
        // no.of components is 0
        Component[] c=p.getComponents();
            for(int i=0;i<c.length;i++)
                // ClassCastException not raised
                // Since only JCheckBoxes are
                // added to the panel
                JCheckBox k=(JCheckBox)c[i];
                // Set the tooltip text
                // This appears mouse is hovered
                // on k
                k.setToolTipText("Checkbox "+(i+1));
        // Add the JPanel

    public static void main(String args[])
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
                new JCheckBoxExample();
In this way, we can simply create a JCheckBox in Java. Isn't that easy? Also see using serialization on JCheckbox

Screenshot of JCheckBox Example

Posted by at on

Tags: Swing Components,