2 Replies Latest reply on Jun 20, 2013 8:56 PM by viklund_anders

    CheckBox - how to set custom icons


      I need to figure out how to set a custom icon for a selected checkbox via the UIManager

      This works for un-selected checkboxes:

      UIManager.put("CheckBox.icon", new ImageIcon("../images/unselectedCheckBoxIcon.png"))); 

      how to change the icon when it is selected?

      I don't know the UIManager property for a selected checkbox icon and using the setSelectedIcon on the checkbox object it self does not seem to work:

      checkbox.setSelectedIcon(new ImageIcon("../images/selectedCheckBoxIcon.png")); 


      Thanks for any ideas how to solve this!!

        • 1. Re: CheckBox - how to set custom icons

          - JCheckBox haven't Icon, there is paintIcon used,


          - carefully with number of pixels, textIconGap, Insets for available Rectangle

          • 2. Re: CheckBox - how to set custom icons

            Hi mKorbel, many thanks for your suggestion!


            For anyone interestsed,


            here is a small example of how I made it working.

            Unfortunately I cannot attatch the icon images that I use, but the selected icon can be downloaded from here, resize it to 32*32, remove the cross in the middle to save it as the unselected icon.




            * Modified sample code taken from here

            * http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingJCheckBoxCheckMarkIcon.htm




            import javax.swing.*;

            import java.awt.*;

            import java.awt.event.*;



            import java.awt.Color;

            import java.awt.Component;

            import java.awt.Graphics;

            import java.awt.GridLayout;

            import java.net.URL;



            import javax.swing.AbstractButton;

            import javax.swing.ButtonModel;

            import javax.swing.Icon;

            import javax.swing.JCheckBox;

            import javax.swing.JFrame;


            public class IconCheckBoxSample {

              public static void main(String[] args) {

                    JFrame frame = new JFrame("Iconizing CheckBox");


                    Icon checked = new CheckBoxIcon();

                    Icon unchecked = new CheckBoxIcon();

                    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);


                    JCheckBox aCheckBox2 = new JCheckBox("Calzone");



                    Icon checkBoxIcon = new CheckBoxIcon();

                    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);

                    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);

                    frame.setLayout(new GridLayout(0,1));





                    frame.setSize(300, 200);






            class CheckBoxIcon implements Icon {

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

                AbstractButton abstractButton = (AbstractButton)component;

                ButtonModel buttonModel = abstractButton.getModel();



                    g.drawImage(createImage("../images/selectedCheckBoxIcon.png","info"), x, y, component);


                    g.drawImage(createImage("../images/unselectedCheckBoxIcon.png","info"), x, y, component);



              public int getIconWidth() {

                return 33;


              public int getIconHeight() {

                return 33;



              protected static Image createImage(String path, String description) {

                    URL imageURL = CheckBoxIcon.class.getResource(path);

                    Image icn = null;



                    if (imageURL == null) {


                            //System.out.println("path: "+path);

                            icn = new ImageIcon (CheckBoxIcon.class.getResource(path.replace("..",""))).getImage();


                                return icn;


                                System.err.println("Resource not found: " + path);

                                return null;



                         return null;

                    } else {

                        return (new ImageIcon(imageURL, description)).getImage();