You now know how to collect text input from users, but there are many occasions where you would rather give users a finite set of choices than have them enter the data in a text component. Using a set of buttons or a list of items tells your users what choices they have. (It also saves you the trouble of error checking.) In this section, you will learn how to program check boxes, radio buttons, lists of choices, and sliders.

Check Boxes

If you want to collect just a "yes" or "no" input, use a check box component. Check boxes automatically come with labels that identify them. The user usually checks the box by clicking inside it, and turns off the check mark by clicking inside the box again. To toggle the check mark, the user can also press the space bar when the focus is in the check box. shows a simple program with two check boxes, one to turn on or off the italic attribute of a font, and the other for boldface. Note that the second check box has focus, as indicated by the rectangle around the label. Each time the user clicks one of the check boxes, we refresh the screen, using the new font attributes.

Check boxes

Java graphics 09fig16

Check boxes need a label next to them to identify their purpose. You give the label text in the constructor.

bold = new JCheckBox("Bold");

You use the setSelected method to turn a check box on or off. For example,

bold.setSelected(true);

The isSelected method then retrieves the current state of each check box. It is false if unchecked; true if checked. When the user clicks on a check box, this triggers an action event. As always, you attach an action listener. In our program, the two buttons share the same action listener.

ActionListener listener = . . .
bold.addActionListener(listener);
italic.addActionListener(listener);

Its actionPerformed method queries the state of the bold and italic check boxes and sets the font of the panel to plain, bold, italic, or both.

public void actionPerformed(ActionEvent event)
{
 int mode = 0;
 if (bold.isSelected()) mode += Font.BOLD;
 if (italic.isSelected()) mode += Font.ITALIC;
 label.setFont(new Font("Serif", mode, FONTSIZE));
}

is the complete program listing for the check box example.Java graphics notes_icon

In AWT, the equivalent component to a JCheckBox is called a Checkbox (with a lowercase "b"). It generates item events, not action events.

Example CheckBoxTest.java

 1. import java.awt.*;
 2. import java.awt.event.*;
 3. import javax.swing.*;
 4.
 5. public class CheckBoxTest
 6. {
 7. public static void main(String[] args)
 8. {
 9. CheckBoxFrame frame = new CheckBoxFrame();
10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11. frame.show();
12. }
13. }
14.
15. /**
16. A frame with a sample text label and check boxes for
17. selecting font attributes.
18. */
19. class CheckBoxFrame extends JFrame
20. {
21. public CheckBoxFrame()
22. {
23. setTitle("CheckBoxTest");
24. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
25.
26. Container contentPane = getContentPane();
27.
28. // add the sample text label
29.
30. label = new JLabel(
31. "The quick brown fox jumps over the lazy dog.");
32. label.setFont(new Font("Serif", Font.PLAIN, FONTSIZE));
33. contentPane.add(label, BorderLayout.CENTER);
34.
35. // this listener sets the font attribute of
36. // the label to the check box state
37.
38. ActionListener listener = new
39. ActionListener()
40. {
41. public void actionPerformed(ActionEvent event)
42. {
43. int mode = 0;
44. if (bold.isSelected()) mode += Font.BOLD;
45. if (italic.isSelected()) mode += Font.ITALIC;
46. label.setFont(new Font("Serif", mode, FONTSIZE));
47. }
48. };
49.
50. // add the check boxes
51.
52. JPanel buttonPanel = new JPanel();
53.
54. bold = new JCheckBox("Bold");
55. bold.addActionListener(listener);
56. buttonPanel.add(bold);
57.
58. italic = new JCheckBox("Italic");
59. italic.addActionListener(listener);
60. buttonPanel.add(italic);
61.
62. contentPane.add(buttonPanel, BorderLayout.SOUTH);
63. }
64.
65. public static final int DEFAULT_WIDTH = 300;
66. public static final int DEFAULT_HEIGHT = 200;
67.
68. private JLabel label;
69. private JCheckBox bold;
70. private JCheckBox italic;
71.
72. private static final int FONTSIZE = 12;
73. }

javax.swing.JCheckBox 1.2

Java graphics api_icon