CuriousCI
CuriousCI

Reputation: 150

Customizing the "Box" in a JCheckBox

I would like to change the look of the Box inside the JCheckBox. I tried to create a "CustomIcon" class which implemenst "Icon", and, using the methods "JCheckBox.setDisabledIcon()" and "JCHeckBox.setDisabledSelectedIcon()" setting the icon with my class, but I didn't get any result. This was the best solution I found after trying to @Override the "BasicCheckBoxUI.paint()" method, and it didn't work too.

CustomIcon class:

{
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ionut Cicio
 */
public class CustomizedIcon implements Icon{
    public int width;
    public int height;

    public Color color;

    public CustomizedIcon(int width, int height, Color color){
        this.width = width;
        this.height = height;
        this.color = color;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y){
        g.setColor(this.color);
        g.fillRect(x, y, width, height);

        g.drawRect(x, y, width, height);
    } 

    @Override
    public int getIconWidth(){
        return this.width;
    }

    @Override
    public int getIconHeight(){
        return this.height;
    }
}

Utilization:

    rememberPasswordCheckBox.setDisabledSelectedIcon(new CustomizedIcon(10, 10, new Color(100, 255, 100)));
    rememberPasswordCheckBox.setDisabledIcon(new CustomizedIcon(10, 10, new Color(255, 100, 100)));

Could you please help me finding the mistake, or explaining me how to do it?

Upvotes: 0

Views: 334

Answers (1)

I changed setDisabledSelectedIcon to setSelectedIcon and setDisabledIcon to setIcon and it worked fine - even for a disabled check box.

Upvotes: 1

Related Questions