mercredi 6 décembre 2017

Custom CheckBox not working

I'm creating a Look and Feel for Java. I'm trying to put an ImageIcon as a checkbox. I have created the icon already in the correct size but I can't seem to figure to get it to work like a checkbox should. It will only display 1 of the 2. I have 2 images, a checked one and a nonchecked (empty) one.

How can I get this working that it acts like a checkbox?

The code:

private void setIcon() {

    ImageIcon nonchecked = new ImageIcon(this.getClass().getResource("images/icon.png"));
    ImageIcon checked = new ImageIcon(this.getClass().getResource("images/checked.png"));

    int HEIGHT = 36;
    int WIDTH = 36;

    Image checkedBox = checked.getImage();
    Image noncheckedBox = nonchecked.getImage();

    BufferedImage bi2 = new BufferedImage(noncheckedBox.getWidth(null), noncheckedBox.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics g2 = bi2.createGraphics();
    g2.drawImage(noncheckedBox, 0, 0, WIDTH, HEIGHT, null);
    ImageIcon noncheckedBox1 = new ImageIcon(bi2);

    BufferedImage bi = new BufferedImage(checkedBox.getWidth(null), checkedBox.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.drawImage(checkedBox, 0, 0, WIDTH, HEIGHT, null);
    ImageIcon checkedBox1 = new ImageIcon(bi);

    UIManager.put("CheckBox.icon", noncheckedBox1);
    UIManager.put("CheckBox.select", checkedBox1);


}

I'm extending BasicCheckBoxUI.




Aucun commentaire:

Enregistrer un commentaire