Customize the Icon in a JCheckBox Component of Java Swing

This section shows you how to customize the icon in a check box component.

Customize the Icon in a JCheckBox Component of Java Swing

This section shows you how to customize the icon in a check box component.

Customize the Icon in a JCheckBox Component of Java Swing

Customize the Icon in a JCheckBox Component of Java Swing

     

This section shows you how to customize the icon in a check box component. That means any icon can be shown where the checked or unchecked box of the check box is situated. The specified icon is looked with the given text for the check box. Image for the pictorial representation is as follows: 

Customized Swing Check Box

Following is the method used in this program:

setIcon(Icon):
This is the method of the JCheckBox class which sets the icon in the check box component of swing. The icon is passed to the
setIcon() method as a parameter. The image for icon is specified in the ImageIcon() constructor of the ImageIcon class. This constructor has been used in this program to create the instance of the Icon class for specifying the image name.

Here is the code of the program:

import javax.swing.*;

public class CustomizedCheckBox{
  public static void main(String[] args){
  JFrame frame = new JFrame("Customized Check Box");
  Icon im = new ImageIcon("cut.gif");
  JCheckBox chk = new JCheckBox("This is the Customized Check Box");
  chk.setIcon(im);
  frame.add(chk);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.