The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JCheckBox  [4 examples]

e755. Customizing the Icons in a JCheckBox Component

The icons used to depict the selected state of a checkbox component can be customized. The simplest customization requires two icons, one to depict the selected state and one to depict the unselected state.
    // Set the unselected state icon
    Icon unselIcon = new ImageIcon("nosel-icon.gif");
    checkbox.setIcon(unselIcon);
    
    // Set the selected state icon
    Icon selIcon = new ImageIcon("sel-icon.gif");
    checkbox.setSelectedIcon(selIcon);
If the checkbox is disabled, grayed out icons are automatically generated for the customized icons. Here's how to customize these disabled icons:
    // Set the unselected state icon
    Icon unselDisIcon = new ImageIcon("nosel-dis-icon.gif");
    checkbox.setDisabledIcon(unselDisIcon);
    
    // Set the selected state icon
    Icon selDisIcon = new ImageIcon("sel-dis-icon.gif");
    checkbox.setDisabledSelectedIcon(selDisIcon);
By default, when the user clicks on a checkbox, the pressed icon (if set) is displayed. Here's how to set it:
    Icon pressedIcon = new ImageIcon("pres-icon.gif");
    checkbox.setPressedIcon(pressedIcon);
Finally, it is possible to display an icon when the cursor is moved over the checkbox. This is called the rollover icon.
    Icon rollIcon = new ImageIcon("roll-icon.gif");
    checkbox.setRolloverIcon(rollIcon);

 Related Examples
e752. Creating a JCheckbox Component
e753. Getting and Setting the State of a JCheckbox Component
e754. Adding an Icon to the Label of a JCheckBox Component

See also: Actions    JButton    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.