Way2Java

Java Checkbox Example Tutorial

Checkbox is another Java component used by the programmer when the user is left with two options only – either yes or no, that is, want or don't want. The check box when selected by the user, returns true and when deselected returns false. The checkbox toggles between two states – true or false. The state can be obtained with getState() method.

The Checkbox generates ItemEvent and is handled by ItemListener.

Following is the class signature

public class Checkbox extends Component implements ItemSelectable, Accessible

Example on Java Checkbox Example Tutorial changing the label text as per the check box selected.
import java.awt.*;
import java.awt.event.*;
public class CheckTest extends Frame implements ItemListener
{
  Checkbox nameBox, boldBox, italicBox, exitBox;
  Label lab;
  public CheckTest()
  {
    nameBox = new Checkbox("Monospaced");
    boldBox = new Checkbox("Bold", true);
    italicBox = new Checkbox("Italic");
    lab = new Label("Way 2 Java");

    exitBox = new Checkbox();
    exitBox.setLabel("Close");
    exitBox.setState(false);
    exitBox.setBackground(Color.cyan);
    exitBox.setForeground(Color.blue);
    exitBox.setFont(new Font("Serif", Font.BOLD, 12));

    nameBox.addItemListener(this);
    boldBox.addItemListener(this);
    italicBox.addItemListener(this);
    exitBox.addItemListener(this);

    add(nameBox, "North");
    add(boldBox, "West");
    add(italicBox, "East");
    add(exitBox, "South");
    add(lab, "Center");

    System.out.println("exit box label: " + exitBox.getLabel());
    System.out.println("exit box state: " + exitBox.getState());

    setTitle("Check Boxes in Action");
    setSize(300, 300);               
    setVisible(true);
  }
  public void itemStateChanged(ItemEvent e)
  {
    String fontName = "";
    int b = 0, i = 0;
 
    if(nameBox.getState() == true)
         fontName = "Monospaced";
    else
         fontName = "Dialog";    

    if(boldBox.getState() == true)
         b = Font.BOLD;
    else
         b = Font.PLAIN;

    if(italicBox.getState() == true)
         i = Font.ITALIC;
    else
         i = Font.PLAIN;

    if(exitBox.getState() == true)
         System.exit(0);

    Font f1 = new Font(fontName, b+i, 20);
    lab.setFont(f1);
  }
  public static void main(String args[])
  {
    new CheckTest();
  }
}

Four check boxes nameBox, boldBox, italicBox, exitBox and one label lab are created.

nameBox = new Checkbox("Monospaced");
boldBox = new Checkbox("Bold", true);

nameBox is set with a label of Monospaced and boldBox is set with Bold and a default state of true (using overloaded constructor). That is, boldBox comes with a default selected state; but no action takes place.

exitBox = new Checkbox();
exitBox.setLabel("Close");
exitBox.setState(false);
exitBox.setBackground(Color.cyan);
exitBox.setForeground(Color.blue);
exitBox.setFont(new Font("Serif", Font.BOLD, 12));

The exitBox is created using default constructor without any label and state set. They are given separately with setLabel() and setState() methods of Checkbox class. As usual, like to any other component, background, foreground and font are set.

System.out.println("exit box label: " + exitBox.getLabel());
System.out.println("exit box state: " + exitBox.getState());

The getLabel() and getState() methods of Checkbox returns the label and state existing with the check box.

Font f1 = new Font(fontName, b+i, 20);
lab.setFont(f1);

Three temporary variables fontName, b and i are created and values are assigned as per the check boxes selected. Using the variables, a font object f1 is created and set to Label lab using setFont() method.

Event Handling

The event generated by Checkbox (known as ItemEvent) is handled by ItemListener. itemStateChanged(ItemEvent) is the only abstract method existing in ItemListener interface and is overridden in the program. All the check boxes are registered with ItemListener using addItemListener() method. The parameter "this" represents the ItemListener. Any check box selected, the itemStateChanged() method is called and code is executed.

Note: ItemListener does not have a corresponding adapter class. Why?