Java Swing How to - Customize disabled icons for JCheckBox








Question

We would like to know how to customize disabled icons for JCheckBox.

Answer

//from  w w  w. j av  a  2 s  .  com

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;

public class Main {
  public static void main(String[] argv) throws Exception {

    JCheckBox checkbox = new JCheckBox();

    // Set the unselected state icon
    Icon unselDisIcon = new ImageIcon("nosel-dis-icon.gif");
    checkbox.setDisabledIcon(unselDisIcon);

    // Set the selected state icon
    Icon selDisIcon = new ImageIcon("sel-dis-icon.gif");
    checkbox.setDisabledSelectedIcon(selDisIcon);

  }
}