datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Creating a JRadioButton

In Swing, the JRadioButton allows the user to select, or un-select, an item which is displayed. Only one of a group of round bullet buttons can be selected, such as selecting one station button on a car radio. You have most likely seen the use of radio buttons in on-line quizzes, tests, or practice web pages.

Each circle is one JRadioButton.
The chosen JRadioButton displays a dot inside the circle.

note
Radio buttons are dependent upon one another.

A "group" of radio buttons is presented to the user and the user is asked to pick only one of the radio button choices. Due to this dependent relationship between the radio buttons, it is necessary to create this "grouped" dependency within the coding by using ButtonGroup.

ButtonGroup group = new ButtonGroup();
group.add(Button1);
group.add(Button2);

The radio buttons are "added" to the ButtonGroup and they are also "added" to the JPanel. The ButtonGroup is NOT "added" to the JPanel.

dividerdash

JRadioButton

This frame shows a mathematical question with three possible solutions. The user must pick only ONE of the choices.
This frame shows that none of the choices has been pre-selected.

mQ1

reddot JRadioButton() - creates unselected radio button (no text, no image)
reddot JRadioButton(Icon image) - creates unselected radio button with image (no text)
reddot JRadioButton(Icon image, boolean selected) - creates a radio button with an image and specifies whether selected or not selected (no text).
reddot JRadioButton(String text) - creates unselected radio button with text
reddot JRadioButtonl(String text, Icon image)
- creates unselected radio button with text and image
reddot JRadioButton(String text, Icon image, boolean selected) - creates radio button with text, image and selection state indicated

Beware The Icon image replaces the "open circle" radio image. If you want the image to appear AFTER the open circle radio button, place the image in a JLabel and place it after the open circle radio button.

dividerdash

Create and Activate Radio Buttons:
 

When creating interactive components, the interface ActionListener will be needed so that the event created by the circle "dot" (in this example) can be activated.
Remember that import java.awt.event.*; will be needed.

reddot Steps to Establishing Active Radio Buttons:
1. Indicate the class "implements ActionListener"
2.
Declare (name) the radio buttons
3. Create
"new" radio buttons and label them
Warning: Do not re-declare these variables! (see more at end of page)
4. Create a new ButtonGroup
5.
Add the radio buttons to the group
6. Attach "action" to the radio buttons ("addActionListener(this)")
7.
Add components to the JPanel
8. Establish
action to be taken method ("actionPerformed")

Remember that when a JRadioButton is "dotted",
an event will be generated and something will happen.

dividerdash

Example: A mathematical computation appears as the question. Three possible choices are listed as the answer. The user must choose ONE of the possible answers. If the user's choice is correct, the question will be replaced with the message "Correct!!". If the user's choice is not correct, the message will be "Sorry, that is incorrect".
 mQ1 mQ2

rb1
  rb2
rb3

dividerdash

Common Error:

RBWarning

Re-declaring variables may create Null Exception Errors - be careful!

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.