Check boxes | CodeGuru

Check boxes

Bruce Eckel’s Thinking in Java Contents | Prev | Next A check box provides a way to make a single on-off choice; it consists of a tiny box and a label. The box typically holds a little ‘x’ (or some other indication that it is set) or is empty depending on whether that item was […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

A


check box provides a way to make a single on-off choice; it consists of a tiny


box and a label. The box typically holds a little ‘x’ (or some


other indication that it is set) or is empty depending on whether that item was


selected.

You’ll


normally create a

Checkbox
using a constructor that takes the label as an argument. You can get and set
the state, and also get and set the label if you want to read or change it
after the
Checkbox
has been created. Note that the capitalization of
Checkbox
is inconsistent with the other controls, which could catch you by surprise
since you might expect it to be “CheckBox.”

Whenever


a


Checkbox

is set or cleared an event occurs, which you can capture the same way you do a


button. The following example uses a


TextArea

to enumerate all the check boxes that have been checked:

//: CheckBox1.java
// Using check boxes
import java.awt.*;
import java.applet.*;
 
public class CheckBox1 extends Applet {
  TextArea t = new TextArea(6, 20);
  Checkbox cb1 = new Checkbox("Check Box 1");
  Checkbox cb2 = new Checkbox("Check Box 2");
  Checkbox cb3 = new Checkbox("Check Box 3");
  public void init() {
    add(t); add(cb1); add(cb2); add(cb3);
  }
  public boolean action (Event evt, Object arg) {
    if(evt.target.equals(cb1))
      trace("1", cb1.getState());
    else if(evt.target.equals(cb2))
      trace("2", cb2.getState());
    else if(evt.target.equals(cb3))
      trace("3", cb3.getState());
    else
      return super.action(evt, arg);
    return true;
  }
  void trace(String b, boolean state) {
    if(state)
      t.appendText("Box " + b + " Setn");
    else
      t.appendText("Box " + b + " Clearedn");
  }
} ///:~ 

The


trace( )

method sends the name of the selected


Checkbox

and its current state to the


TextArea

using

appendText( )
so you’ll see a cumulative list of the checkboxes that were selected and
what their state is.
Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.