The concept of the
radio buttons in GUI programming comes from the mechanical buttons of old-fashioned tube car radios: When we press a button, the other buttons bounce. So it allows us to force a single choice from a multitude of choices.
AWT does not have a separate class that describes radio buttons; instead, the duplicate checkbox. However, place the check box in the radio buttons group (and modify its shape to make it look different from the General check box) We must use a special builder like an independent variable to function on the CheckboxGroup object. (We can also call the Setcheckboxgroup () method After creating the check box.)
A checkbox group does not have a builder's argument; the only reason it exists is by aggregating some check boxes into the radio button group. A check box object must set its state to true before we attempt to display the radio button group, otherwise we will get an exception at runtime. If we set more than one radio buttons to true, only the last one can be set to true.
Here's a simple example of using radio buttons. Note that we can capture radio buttons events like other components:
: Radiobutton1.java
//Using radio buttons
import java.awt.*;
Import java.applet.*;
public class RadioButton1 extends Applet {
TextField t =
new TextField ("Radio button 2");
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;
}
///:~
The state of the display is a text field in use. This field is set to not editable because it is used to display data instead of collection. This demonstrates a desirable way to use labels. Note that the text in the field is initialized by the first selected radio button "Radio button 2".
We can have quite a few check box groups in the form.