Icon CheckBox Demo : CheckBox Button « Swing JFC « Java






Icon CheckBox Demo

Icon CheckBox Demo
  
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Polygon;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class IconCheckBoxSample {
  private static class CheckBoxIcon implements Icon {
    private ImageIcon checkedIcon = new ImageIcon("Plus.gif");

    private ImageIcon uncheckedIcon = new ImageIcon("Minus.gif");

    public void paintIcon(Component component, Graphics g, int x, int y) {
      AbstractButton abstractButton = (AbstractButton) component;
      ButtonModel buttonModel = abstractButton.getModel();
      g.translate(x, y);
      ImageIcon imageIcon = buttonModel.isSelected() ? checkedIcon
          : uncheckedIcon;
      Image image = imageIcon.getImage();
      g.drawImage(image, 0, 0, component);
      g.translate(-x, -y);
    }

    public int getIconWidth() {
      return 20;
    }

    public int getIconHeight() {
      return 20;
    }
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon checked = new DiamondIcon(Color.black, true);
    Icon unchecked = new DiamondIcon(Color.black, false);
    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
    aCheckBox1.setSelectedIcon(checked);
    JCheckBox aCheckBox2 = new JCheckBox("Calzone");
    aCheckBox2.setIcon(unchecked);
    aCheckBox2.setSelectedIcon(checked);
    Icon checkBoxIcon = new CheckBoxIcon();
    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));
    contentPane.add(aCheckBox1);
    contentPane.add(aCheckBox2);
    contentPane.add(aCheckBox3);
    contentPane.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    } else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}

           
         
    
  








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.Flat CheckBoxFlat CheckBox
11.Customizing the Icons in a JCheckBox Component
12.Customize these disabled icons
13.Set pressed icon
14.Display an icon when the cursor is moved over the checkbox. This is called the rollover icon.
15.Adding an Icon to the Label of a JCheckBox Component
16.Getting and Setting the State of a JCheckbox Component
17.Creating a JCheckbox Component
18.Check boxes with item changed event.
19.Get or set the selection state of JCheckBox
20.Customize JCheckBox icons
21.Radio button, ComboBoxRadio button, ComboBox
22.Using CheckBox ActionListener to controll font