Customize JCheckBox icons : CheckBox Button « Swing JFC « Java






Customize JCheckBox icons

  

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class JCheckBoxCustomIcon extends JFrame {
  public JCheckBoxCustomIcon() {
    setSize(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    // Set default icon for checkbox
    checkBox.setIcon(new ImageIcon("icon.png"));
    // Set selected icon when checkbox state is selected
    checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
    // Set disabled icon for checkbox
    checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
    // Set disabled-selected icon for checkbox
    checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
    // Set checkbox icon when checkbox is pressed
    checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
    // Set icon when a mouse is over the checkbox
    checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
    // Set icon when a mouse is over a selected checkbox
    checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

    getContentPane().add(checkBox);
  }

  public static void main(String[] args) {
    new JCheckBoxCustomIcon().setVisible(true);
  }
}

   
    
  








Related examples in the same category

1.JCheckBox is a widget that has two states. On and Off.
2.Swing CheckBoxesSwing CheckBoxes
3.CheckBox Demo 2CheckBox Demo 2
4. How to use the check box button How to use the check box button
5.CheckBox MnemonicCheckBox Mnemonic
6.Bad Checkbox UIBad Checkbox UI
7.React to menu action and checkbox menuReact to menu action and checkbox menu
8.Swing CheckBox DemoSwing CheckBox Demo
9.CheckBox Item ListenerCheckBox Item Listener
10.Icon CheckBox DemoIcon CheckBox Demo
11.Flat CheckBoxFlat CheckBox
12.Customizing the Icons in a JCheckBox Component
13.Customize these disabled icons
14.Set pressed icon
15.Display an icon when the cursor is moved over the checkbox. This is called the rollover icon.
16.Adding an Icon to the Label of a JCheckBox Component
17.Getting and Setting the State of a JCheckbox Component
18.Creating a JCheckbox Component
19.Check boxes with item changed event.
20.Get or set the selection state of JCheckBox
21.Radio button, ComboBoxRadio button, ComboBox
22.Using CheckBox ActionListener to controll font