Swing CheckBoxes : CheckBox Button « Swing JFC « Java

Swing CheckBoxes

Swing CheckBoxes

// : c14:SwingCheckBoxes.java

// <applet code=SwingCheckBoxes width=200 height=200></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SwingCheckBoxes extends JApplet {
  private JTextArea t = new JTextArea(6, 15);

  private JCheckBox cb1 = new JCheckBox("Check Box 1"), cb2 = new JCheckBox(
      "Check Box 2"), cb3 = new JCheckBox("Check Box 3");

  public void init() {
    cb1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        trace("1", cb1);
    cb2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        trace("2", cb2);
    cb3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        trace("3", cb3);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(new JScrollPane(t));

  private void trace(String b, JCheckBox cb) {
    if (cb.isSelected())
      t.append("Box " + b + " Set\n");
      t.append("Box " + b + " Cleared\n");

  public static void main(String[] args) {
    run(new SwingCheckBoxes(), 200, 200);

  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setSize(width, height);
} ///:~


Related examples in the same category

1.JCheckBox is a widget that has two states. On and Off.
2.CheckBox Demo 2CheckBox Demo 2
3. How to use the check box button How to use the check box button
4.CheckBox MnemonicCheckBox Mnemonic
5.Bad Checkbox UIBad Checkbox UI
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