JRadioButton in java




                                JRadioButton in Java

We use the JRadioButton class to create a radio button. Radio button is use to select one option from multiple options. An implementation of a radio button - an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)


Note: The ButtonGroup object is a logical grouping and is not a physical grouping.


Uses:
It is used to choose one option from multiple options. It is widely used in exam systems or quiz.



JRadioButton class declaration

The declaration for javax.swing.JRadioButton class is shown below:-


public class JRadioButton extends JToggleButton implements Accessible  


How to make a Button Group:-
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. 
  • Create a ButtonGroup instance by using “ButtonGroup()” Method.
  • ButtonGroup A= new ButtonGroup()
  • Now add buttons in a Group “A”, with the help of “add()” Method. 
Example:


A.add(Button1);

A.add(Button2);


JRadioButton() Constructor Detail:-

  • JRadioButton
public JRadioButton()
Creates an initially unselected radio button with no set text.

  • JRadioButton
public JRadioButton(Icon icon)

Creates an initially unselected radio button with the specified image but no text.

Parameters:

icon - the image that the button should display


  • JRadioButton
  • public JRadioButton(Action a)
    Creates a radiobutton where properties are taken from the Action supplied.

  • JRadioButton
  • public JRadioButton(Icon icon,boolean selected)
Creates a radio button with the specified image and selection state, but no text.
Parameters:
icon - the image that the button should display
selected - if true, the button is initially selected; otherwise, the button is initially unselected

  • JRadioButton
public JRadioButton(String text)
Creates an unselected radio button with the specified text.
Parameters:
text - the string displayed on the radio button

  • JRadioButton
  • public JRadioButton(String text,   boolean selected)
Creates a radio button with the specified text and selection state.

Parameters:
text - the string displayed on the radio button.
selected - if true, the button is initially selected; otherwise, the button is initially unselected.

  • JRadioButton
  • public JRadioButton(String text,   Icon icon)
Creates a radio button that has the specified text and image, and that is initially unselected.

Parameters:
text - the string displayed on the radio button.
icon - the image that the button should display.

  • JRadioButton
  • public JRadioButton(String text,  Icon icon,boolean selected)
Creates a radio button that has the specified text, image, and selection state.
Parameters:
text - the string displayed on the radio button.
icon - the image that the button should display.


 // Program to illustrate the use of JRadioButton in java.

import javax.swing.*;    
public class RadioButtonExample {    
JFrame f;    
RadioButtonExample(){    
f=new JFrame();     
JRadioButton r1=new JRadioButton("A) Male");    
JRadioButton r2=new JRadioButton("B) Female");    
r1.setBounds(75,50,100,30);    
r2.setBounds(75,100,100,30);    
ButtonGroup bg=new ButtonGroup();    
bg.add(r1);bg.add(r2);    
f.add(r1);f.add(r2);      
f.setSize(300,300);    
f.setLayout(null);    
f.setVisible(true);    
}    
public static void main(String[] args) {    
    new RadioButtonExample();    
}    
}  
  
Output :-
Fig:Example of JRadioButton






                                       for any query feel free to comment.





Comments