Java JRadioButton
JRadioButton is used to create radio button.JRadioButtons are used to select one option from several options unlike JCheckBox that you can select multiple options.
A group of radio buttons should be added to one ButtonGroup in order to select one.
A group of radio buttons should be added to one ButtonGroup in order to select one.
How to Declare JRadioButton class
This is how you declare javax.swing.JRadioButton.
[public class JRadioButton extends JToggleButton implements Accessible]
Constructors used in JRadioButton.
- JRadioButton()-creates radio button that is unselected.
- JRadioButton(String s)-creates radio button with a certain text.
- JRadioButton(String s,boolean selected)-creates radio button that is selected and with a certain text.
JRadioButtons commonly used methods.
- void setText(String s)-used to specify radio button text.
- String getText()-returns radio button text.
- void setEnabled(boolean value)-used to enable or disable the button.
- void setIcon(Icon icon)-used to add icon to the radio button.
- Icon getIcon()-returns icon used in the radio button.
- void setMnemonic(int a)-use to set radio button mnemonic.
- void addActionListener(ActionListener l)-used to add actionlistener.
Java JRadioButton Example
[
import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; public class RadioButtonExample extends JFrame { RadioButtonExample() { super("JRadioButton Example"); JRadioButton button1=new JRadioButton("A) Male"); JRadioButton button2=new JRadioButton("B) Female"); button1.setBounds(75,50,100,30); button2.setBounds(75,100,100,30); ButtonGroup bg=new ButtonGroup(); bg.add(button1);bg.add(button2); add(button1); add(button2); setLocationRelativeTo(null); setSize(400,400); setLayout(null); setVisible(true); } public static void main(String args[]) { new RadioButtonExample(); } }
]
Output:
Java JRadioButton Example with ActionListener
[
import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class RadioButtonExample extends JFrame implements ActionListener{ public JRadioButton button1,button2; public JButton save; RadioButtonExample() { super("JRadioButton Example"); button1=new JRadioButton("A) Male"); button2=new JRadioButton("B) Female"); button1.setBounds(75,50,100,30); button2.setBounds(75,100,100,30); ButtonGroup bg=new ButtonGroup(); bg.add(button1);bg.add(button2); add(button1); add(button2); save=new JButton("Save"); save.setBounds(75,150,100,30); add(save); save.addActionListener(this); setLocationRelativeTo(null); setSize(400,400); setLayout(null); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event) { if(button1.isSelected()) { JOptionPane.showMessageDialog(this, "You Gender is Male", "JRadioButton Example",JOptionPane.INFORMATION_MESSAGE); } if(button2.isSelected()) { JOptionPane.showMessageDialog(this, "You Gender is Female", "JRadioButton Example",JOptionPane.INFORMATION_MESSAGE); } } public static void main(String args[]) { new RadioButtonExample(); } }
]
Output
Code Explanation:
Above program introduces several java.swing class components These are :-
The program extends class JFrame and implements ActionListener interface.
Note:Once you extends a class,you can be able to access all the class methods.
JFrame methods include setSize,setLayout,setDefaultCloseOperation etc.
If a class is not abstract and it implements a certain interface,you are required to override all interface abstract methods.
Code [button1.isSelected()] checks if the radio button is selected.