new JCheckBox(String text, Icon normal) : JCheckBox « javax.swing « Java by API






new JCheckBox(String text, Icon normal)

  
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel implements ItemListener {
  public MainClass() {
    // Add check boxes to the content pane.
    Icon normal = new MyIcon(Color.red);
    Icon rollover = new MyIcon(Color.YELLOW);
    Icon selected = new MyIcon(Color.BLUE);

    JCheckBox cb = new JCheckBox("www.java2s.com", normal);
    cb.setRolloverIcon(rollover);
    cb.setSelectedIcon(selected);
    cb.addItemListener(this);
    add(cb);

  }

  public void itemStateChanged(ItemEvent ie) {
    JCheckBox cb = (JCheckBox) ie.getItem();
    int state = ie.getStateChange();
    if (state == ItemEvent.SELECTED)
      System.out.println(cb.getText() + " selected");
    else
      System.out.println(cb.getText() + " cleared");
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }

}

class MyIcon implements Icon {
  Color cl;

  public MyIcon(Color c) {
    cl = c;
  }

  public int getIconWidth() {
    return 32;
  }

  public int getIconHeight() {
    return 32;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(cl);
    g.drawString("java2s.com", 0, 20);
  }
}

           
         
    
  








Related examples in the same category

1.new JCheckBox(Action a)
2.new JCheckBox(String text)
3.new JCheckBox(String text, boolean selected)
4.JCheckBox: addActionListener(ActionListener lis)
5.JCheckBox: addChangeListener(ChangeListener lis)
6.JCheckBox: addComponentListener(ComponentListener l)
7.JCheckBox: addItemListener(ItemListener lis)
8.JCheckBox: isSelected()
9.JCheckBox: setBorderPaintedFlat(boolean b)
10.JCheckBox: setDisabledIcon(Icon disabledIcon)
11.JCheckBox: setDisabledSelectedIcon(Icon disabledSelectedIcon)
12.JCheckBox: setEnabled(boolean b)
13.JCheckBox: setIcon(Icon defaultIcon)
14.JCheckBox: setMnemonic(int mnemonic)
15.JCheckBox: setPressedIcon(Icon pressedIcon)
16.JCheckBox: setRolloverIcon(Icon i)
17.JCheckBox: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
18.JCheckBox: setSelected(boolean b)
19.JCheckBox: setSelectedIcon(Icon i)
20.JCheckBox: setText(String text) (HTML)
21.JCheckBox: setToolTipText(String text)