All for Joomla All for Webmasters

JRadioButton class

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.

Commonly used Constructors of JRadioButton class:

  • 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 of AbstractButton class:

1) public void setText(String s): is used to set specified text on button.
2) public String getText(): is used to return the text of the button.
3) public void setEnabled(boolean b): is used to enable or disable the button.
4) public void setIcon(Icon b): is used to set the specified Icon on the button.
5) public Icon getIcon(): is used to get the Icon of the button.
6) public void setMnemonic(int a): is used to set the mnemonic on the button.
7) public void addActionListener(ActionListener a): is used to add the action listener to this object.

Note: The JRadioButton class extends the JToggleButton class that extends AbstractButton class.

Example of JRadioButton class:

  1. import javax.swing.*;
  2. public class Radio {
  3. JFrame f;
  4. Radio(){
  5. f=new JFrame();
  6. JRadioButton r1=new JRadioButton(“A) Male”);
  7. JRadioButton r2=new JRadioButton(“B) FeMale”);
  8. r1.setBounds(50,100,70,30);
  9. r2.setBounds(50,150,70,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 Radio();
  19. }
  20. }

ButtonGroup class:

The ButtonGroup class can be used to group multiple buttons so that at a time only one button can be selected.

JRadioButton example with event handling

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. class RadioExample extends JFrame implements ActionListener{
  4. JRadioButton rb1,rb2;
  5. JButton b;
  6. RadioExample(){
  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 RadioExample();
  31. }}
PinIt
submit to reddit

Leave a Reply

Top