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 (http://developer.java.sun.com/developer/bugParade/bugs/4449413.html) 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.

Main.java:

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

      getContentPane().setLayout(null); 
      getContentPane().add(cbox);
      cbox.setBounds(10, 20, 250, 30);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String args[]) throws Exception {
      Main main = new Main();
      main.setSize(300, 100);
      main.setVisible(true);
   }
}
  
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.setColor(UIManager.getColor("MyCheckBox.checkClickBackground"));
	    g.fillRect( x, y, controlSize-1, controlSize-1);
         } else {
            g.setColor(UIManager.getColor("MyCheckBox.checkBackground"));
            g.fillRect( x, y, controlSize-1, controlSize-1);
         }
 
         g.setColor(UIManager.getColor("MyCheckBox.checkForeground"));
 
      } else {
         g.setColor(UIManager.getColor("MyCheckBox.checkDisabled"));
         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();
   }
 }