Radio buttons | CodeGuru

Radio buttons

Bruce Eckel’s Thinking in Java Contents | Prev | Next The concept of a radio button in GUI programming comes from pre-electronic car radios with mechanical buttons: when you push one in, any other button that was pressed pops out. Thus it allows you to force a single choice among many. The AWT does not […]

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

The


concept of a

radio
button in GUI programming comes from pre-electronic car radios with mechanical
buttons: when you push one in, any other button that was pressed pops out. Thus
it allows you to force a single choice among many.

The


AWT does not have a separate class to represent the radio button; instead it


reuses the

Checkbox.
However, to put the
Checkbox
in a radio button group (and to change its shape so it’s visually
different from an ordinary
Checkbox)
you must use a special constructor that takes a
CheckboxGroup
object as an argument. (You can also call
setCheckboxGroup( )
after the
Checkbox
has been created.)

A


CheckboxGroup

has


no constructor argument; its sole reason for existence is to collect some


Checkbox

es


into a group of radio buttons. One of the


Checkbox

objects must have its state set to


true

before you try to display the group of radio buttons; otherwise you’ll


get an exception at run time. If you try to set more than one radio button to


true

then only the final one set will be


true

.

Here’s


a simple example of the use of radio buttons. Note that you capture radio


button events like all others:

//: RadioButton1.java
// Using radio buttons
import java.awt.*;
import java.applet.*;
 
public class RadioButton1 extends Applet {
  TextField t =
    new TextField("Radio button 2", 30);
  CheckboxGroup g = new CheckboxGroup();
  Checkbox
    cb1 = new Checkbox("one", g, false),
    cb2 = new Checkbox("two", g, true),
    cb3 = new Checkbox("three", g, false);
  public void init() {
    t.setEditable(false);
    add(t);
    add(cb1); add(cb2); add(cb3);
  }
  public boolean action (Event evt, Object arg) {
    if(evt.target.equals(cb1))
      t.setText("Radio button 1");
    else if(evt.target.equals(cb2))
      t.setText("Radio button 2");
    else if(evt.target.equals(cb3))
      t.setText("Radio button 3");
    else
      return super.action(evt, arg);
    return true;
  }
} ///:~ 

To


display the state, an text field is used. This field is set to non-editable


because it’s used only to display data, not to collect it. This is shown


as an alternative to using a


Label

.


Notice the text in the field is initialized to “Radio button 2”


since that’s the initial selected radio button.

You


can have any number of


CheckboxGroup

s


on a form.


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.