3

How do I change or modify the color of the JCheckBox symbol (not the text property). I'm testing UIManager.put("CheckBox.selected", Color.RED) without success.

Can someone help?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
BicaBicudo
  • 307
  • 1
  • 8
  • 20
  • Try `"CheckBox.select"`, although it is ignored by some UI delegates. – trashgod Apr 27 '16 at 01:34
  • 1
    `JCheckBox` uses a `icon` for both the selected and unselected icons (both must be set), so the only way to change there color would be to change the icon – MadProgrammer Apr 27 '16 at 03:03

3 Answers3

4

JCheckBox uses the icon and selectedIcon to represent the "selected" and "unselected" states.

The only way you can change those is to use your own icon. For example...

CustomCheckBox

public class TestPane extends JPanel {
    
    public TestPane() {
        try {
            JCheckBox cb = new JCheckBox();
            cb.setSelectedIcon(new ImageIcon(ImageIO.read(...)));
            cb.setIcon(new ImageIcon(ImageIO.read(...)));
            cb.setBackground(Color.RED);
            cb.setOpaque(true);
            add(cb);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
}

If you just want to change the background color of JCheckBox instead, you'll need to make it opaque first:

cb.setBackground(Color.RED);
cb.setOpaque(true);

because they are transparent by default.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you - Opaque was what I needed. I'll also report for the other seekers there is no margin, etc on check-box labels. So some more finesse is required than just a colour change. _Nice_ – will Feb 15 '17 at 00:54
0
  UIManager.put("CheckBox.focus",Color.RED); //on focus
  UIManager.put("CheckBox.select",Color.RED) //on select

  checkBox1.setForeground(Color.RED); //you can call this in the combobox action listner
  checkbox1.setBackground(Color.Blue); //changing the background color

can u check with this code.

Priyamal
  • 2,919
  • 2
  • 25
  • 52
0

for change checkbox inside color you can use this in main

try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        System.out.println("Unable to set LookAndFeel");
    }

check box and radio button inside color

cnmeysam
  • 13
  • 6