Changing the color of the check of my JCheckBox

This is all tied into the particular L and F implementation of the JCheckBox. The check color is always black (or gray, if disabled). If your background happens to be black, you can’t see the check. This is a bug ( in progress.

This example shows you a bit of a workaround by implementing another version of CheckBoxIcon, the UI class used to paint the rectangle and the checkmark. I defined a couple new properties: MyCheckBox.checkBackground, MyCheckBox.checkClickBackground, MyCheckBox.checkForeground, MyCheckBox.checkDisabled. They all take a Color object.

import javax.swing.plaf.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Main extends JFrame
   public Main() throws Exception {
      UIManager.put("CheckBox.icon", new MyCheckBoxIcon());
      UIManager.put("MyCheckBox.checkDisabled", Color.gray); 
      JCheckBox cbox = new JCheckBox("This checkbox has a colored check");

      cbox.setBounds(10, 20, 250, 30);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
   public static void main(String args[]) throws Exception {
      Main main = new Main();
      main.setSize(300, 100);
class MyCheckBoxIcon implements Icon, UIResource, Serializable {
   protected int getControlSize() { return 13; }
   public void paintIcon(Component c, Graphics g, int x, int y) {
      JCheckBox cb = (JCheckBox)c;
      ButtonModel model = cb.getModel();
      int controlSize = getControlSize();
      boolean drawCheck = model.isSelected();
      if (model.isEnabled()) {
         if (model.isPressed() && model.isArmed()) {
	    g.fillRect( x, y, controlSize-1, controlSize-1);
         } else {
            g.fillRect( x, y, controlSize-1, controlSize-1);
      } else {
         g.drawRect( x, y, controlSize-1, controlSize-1);
      if (model.isSelected()) {
         drawCheck(c, g, x, y);
   protected void drawCheck(Component c, Graphics g, int x, int y) {
      int controlSize = getControlSize();
      g.fillRect( x+3, y+5, 2, controlSize-8 );
      g.drawLine( x+(controlSize-4), y+3, x+5, y+(controlSize-6) );
      g.drawLine( x+(controlSize-4), y+4, x+5, y+(controlSize-5) );
   public int getIconWidth() {
      return getControlSize();
   public int getIconHeight() {
      return getControlSize();