Java Programming Code Examples
Java > Java AWT Code Examples
Creating radio buttons using CheckboxGroup and Checkbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Creating radio buttons using CheckboxGroup and Checkbox
import java.applet.Applet;
import java.awt.*;
public class RadioButton extends Applet {
private TextField t;
private Font f;
private CheckboxGroup radio;
private Checkbox radioBold, radioItalic,
radioPlain;
public void init()
{
t = new TextField( "Sample Text", 40 );
// instantiate checkbox group (i.e. radio buttons)
radio = new CheckboxGroup();
add( t ); // add textfield
// instantiate radio button objects
add( radioPlain = new Checkbox( "Plain", radio, true ) );
add( radioItalic = new Checkbox( "Italic", radio, false ) );
add( radioBold = new Checkbox( "Bold", radio, false ) );
}
public boolean action( Event e, Object o )
{
int style;
// Check for Checkbox event
if ( e.target instanceof Checkbox) {
// test state of radio buttons
if ( radioPlain.getState() == true )
style = Font.PLAIN;
else if ( radioItalic.getState() == true )
style = Font.ITALIC;
else
style = Font.BOLD;
f = new Font( "TimesRoman", style, 14 );
t.setFont( f );
}
return true;
}
}