Java JRadioButton

Java JRadioButton

The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.

It should be added in ButtonGroup to select one radio button only.

JRadioButton class declaration

Let’s see the declaration for javax.swing.JRadioButton class.

  1. public class JRadioButton extends JToggleButton implements Accessible

Commonly used Constructors:

Constructor Description
JRadioButton() Creates an unselected radio button with no text.
JRadioButton(String s) Creates an unselected radio button with specified text.
JRadioButton(String s, boolean selected) Creates a radio button with the specified text and selected status.

Commonly used Methods:

Methods Description
void setText(String s) It is used to set specified text on button.
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener(ActionListener a) It is used to add the action listener to this object.

 

Java JRadioButton Example

  1. import javax.swing.*;
  2. public class RadioButtonExample {
  3. JFrame f;
  4. RadioButtonExample(){
  5. f=new JFrame();
  6. JRadioButton r1=new JRadioButton(“A) Male”);
  7. JRadioButton r2=new JRadioButton(“B) Female”);
  8. r1.setBounds(75,50,100,30);
  9. r2.setBounds(75,100,100,30);
  10. ButtonGroup bg=new ButtonGroup();
  11. bg.add(r1);bg.add(r2);
  12. f.add(r1);f.add(r2);
  13. f.setSize(300,300);
  14. f.setLayout(null);
  15. f.setVisible(true);
  16. }
  17. public static void main(String[] args) {
  18.     new RadioButtonExample();
  19. }
  20. }

Output:

JAVA Jradiobutton 1


Java JRadioButton Example with ActionListener

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. class RadioButtonExample extends JFrame implements ActionListener{
  4. JRadioButton rb1,rb2;
  5. JButton b;
  6. RadioButtonExample(){
  7. rb1=new JRadioButton(“Male”);
  8. rb1.setBounds(100,50,100,30);
  9. rb2=new JRadioButton(“Female”);
  10. rb2.setBounds(100,100,100,30);
  11. ButtonGroup bg=new ButtonGroup();
  12. bg.add(rb1);bg.add(rb2);
  13. b=new JButton(“click”);
  14. b.setBounds(100,150,80,30);
  15. b.addActionListener(this);
  16. add(rb1);add(rb2);add(b);
  17. setSize(300,300);
  18. setLayout(null);
  19. setVisible(true);
  20. }
  21. public void actionPerformed(ActionEvent e){
  22. if(rb1.isSelected()){
  23. JOptionPane.showMessageDialog(this,“You are Male.”);
  24. }
  25. if(rb2.isSelected()){
  26. JOptionPane.showMessageDialog(this,“You are Female.”);
  27. }
  28. }
  29. public static void main(String args[]){
  30. new RadioButtonExample();
  31. }}

Output:

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java JRadioButton The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz. It should be added in ButtonGroup to select one radio button only. JRadioButton class declaration Let’s see the declaration for javax.swing.JRadioButton class. public class JRadioButton extends JToggleButton implements Accessible Commonly […]

Tags:

....
...