We use the JRadioButton class to create a radio button. Radio button is use to select one option from multiple options. It is used in filling forms, online objective papers and quiz.
We add radio buttons in a ButtonGroup so that we can select only one radio button at a time. We use "ButtonGroup" class to create a ButtonGroup and add radio button in a group.
Methods Used :
- JRadioButton() : Creates a unselected RadioButton with no text.
Example:
JRadioButton j1 = new JRadioButton()
- JButton(String s) : Creates a JButton with a specific text.
Example:
JButton b1 = new JButton("Button")
- JLabel(String s) : Creates a JLabel with a specific text.
Example:
JLabel L = new JLabel("Label 1")
- ButtonGroup() : Use to create a group, in which we can add JRadioButton. We can select only one JRadioButton in a ButtonGroup.
Steps to Group the radio buttons together.
- isSelected() : it will return a Boolean value true or false, if a JRadioButton is selected it Will return true otherwise false.
Example:
JRadioButton.isSelected()
- Set(...) and Get(...) Methods :
i) Set and get are used to replace directly accessing member variables from external classes.
ii) Instead of accessing class member variables directly, you define get methods to access these variables, and set methods to modify them.
Description of some functions used in program, is given in this link :
Functions Description
Program 1 : JRadioButton Without ActionListener
Output:
👁 Image
Program 2 : JRadioButton With ActionListener
Output:
👁 Image
After pressing Button "click".
👁 Image
Program 3 Program to create a simple group of radio buttons (with image )and add item listener to them