Bad Checkbox UI : CheckBox Button « Swing JFC « Java

Bad Checkbox UI

Bad Checkbox UI

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicCheckBoxUI;

public class MyCheckBoxUI extends BasicCheckBoxUI implements, MouseListener {

  private final static MyCheckBoxUI buttonUI = new MyCheckBoxUI();

  public MyCheckBoxUI() {

  public static ComponentUI createUI(JComponent c) {
    return buttonUI;

  public void installUI(JComponent c) {

  public void uninstallUI(JComponent c) {

  public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();
    Dimension d = b.getSize();

    FontMetrics fm = g.getFontMetrics();
    g.drawString("Am I a check box", 10, 10);


  public void mouseClicked(MouseEvent e) {

  public void mousePressed(MouseEvent e) {

  public void mouseReleased(MouseEvent e) {

  public void mouseEntered(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();

  public void mouseExited(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();
  public static void main(String[] argv) {
    JFrame f = new JFrame();
    f.setSize(400, 300);
    f.getContentPane().setLayout(new FlowLayout());

    JPanel p = new JPanel();
    JCheckBox bt1 = new JCheckBox("Click Me");
    bt1.setUI(new MyCheckBoxUI());
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {



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.React to menu action and checkbox menuReact to menu action and checkbox menu
7.Swing CheckBox DemoSwing CheckBox Demo
8.CheckBox Item ListenerCheckBox Item Listener
9.Icon CheckBox DemoIcon CheckBox Demo
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