Skip to main content

How to set the color of the inside of a CheckBox (content area?)

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2010-09-09

I have a requirement to turn the color of a component green if a user has changed the value from its default. (So they can easily see what they have modified on the screen.) Setting the background color works fine for TextFields, but for CheckBoxes setBackground changes the color around the checkbox not inside of the box where the check is. Is there an easy way to get the color of that area to change? Or even a hard way like using paint? Is that area called the content area?
I am also having a problem with ComboBoxes. Changing the background color changes the color of the area with the down arrow and not the area behind the text. Is there a way to change that?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2011-06-09

// Make an Icon class to use in the selected checkbox

public class MyCheckBoxIcon implements Icon {

private Color color;

private int width, height;

MyCheckBoxIcon(Color iconColor) {

color = iconColor;

Icon icon = UIManager.getIcon("CheckBox.icon");

width = icon.getIconWidth();

height = icon.getIconHeight();


public void paintIcon(Component c, Graphics g, int x, int y) {


g.fillRect(x, y, width, height);


public int getIconWidth() {

return width;


public int getIconHeight() {

return height;



// Make a JFrame to display our nifty checkbox

public class ModifiedCheckBoxDemo extends JFrame {


JCheckBox checkBox;


public ModifiedCheckBoxDemo () {





private void initComponents() {


Icon normal = UIManager.getIcon("CheckBox.icon");

Icon selected = new MyCheckBoxIcon(;

checkBox = new JCheckBox("Test", normal);




public static void main(String[] args) {

new ModifiedCheckBoxDemo().setVisible(true);