Add Icon to a JCheckBox in Java

Description

The following code shows how to add Icon to a JCheckBox.

Example


import java.awt.FlowLayout;
/* w ww.ja  va2 s  .com*/
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {

    JFrame frame = new JFrame("JCheckBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox ac = new JCheckBox("A/C");

    Icon rollIcon = new ImageIcon("a.gif");
    ac.setRolloverIcon(rollIcon);
    
    
    frame.add(ac);
    frame.pack();
    frame.setVisible(true);
  }
}

The code above generates the following result.

Add Icon to a JCheckBox in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer