Swing Components / Check Boxes & Radio Buttons
Subtitles of the Movie
Up until now, all the buttons that we've dealt with have been momentary. When you click on one with a mouse, the action event was transmitted and when you let go, the button popped back to normal. But there are two kinds of buttons that don't pop back. When you push them, they stay pushed. The first of these is called a checkbox. Checkboxes show up on the display as the list of items and you can choose as many of them as you'd like. This example isn't very fancy. It just shows you the bare essentials of checkboxes. You will need to implement the item listener interface only if you want to receive notification of every click of the mouse on a checkbox and that's not absolutely necessary. For some applications, it is sufficient to only read the settings of the checkboxes later when the information is needed. This example reads the settings when they are made. Now, this program uses a BoxLayout with a label at the top and a list of checkboxes below it. The label indicates which boxes have been checked. The checkboxes are labeled A, B, C and D. Each checkbox is constructed with a label starting with its letter. At the outset, the checkboxes are all set to being selected or checked. This object is listed as the item listener for the checkbox, so we'll be notified of all its changes. This is the method that's called each time the state of a checkbox changes. All it does is build a string of text for the label that indicates which of the checkboxes are selected and here's how it works. It's a very simple window with no borders or margins. That was to keep the example source code as simple as possible. The four checkboxes are named for animals. Well, except for that last one. I couldn't think of an animal that starts with D, but there was a guy in my school who I always thought was kind of a gorilla, but you can see how changing and selecting and deselecting the buttons changes the label at the top. The other kind of button you can set is the radio button. In a group of radio buttons, only one can be selected. Here again you only need to implement the action listener interface if you want to track each click of the radio button. It's usually easier just to wait til the end and then read them all at once. The layout is the same as it was before. A label at the top is used to indicate which of the buttons has been selected. A button group object is constructed. The radio buttons are all related to one another and the way you relate them is by adding them to the button group. The button group object will automatically deselect all the radio buttons whenever you select one. Each time you create a radio button, you specify whether it's the one to be selected true or false and you add it to the group of radio buttons that are to work all together. All the radio buttons but one should be unselected. Here at the very bottom is the method that's called every time the radio button is selected by the mouse. The label is assigned text indicating which button has been selected. Now, this method call returns the button that has been selected. The information is used to print out the name of the button. Here's how it looks as it runs. As you can see, only one button at a time can be selected. Also notice that the printed button name is the name of the one selected. It's never the one that becomes deselected.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 