Java


Java Tutorial


Admission Enquiry Form

  

Java Swing JRadioButton

Java Swing JRadioButton

JRadioButton class is used to create radio button.Radio buttons allow the user to make a single choice from a set of options.
It should be added in ButtonGroup to select one radio button only.

Constructors used with JRadioButton

Type of Constructor Constructor Description
public 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.
public JRadioButton(Icon icon) Creates a radio button with an icon.
public JRadioButton(String text, Icon icon, boolean selected) Creates a radio button which displays both text and icon. The state of radio button can be initialized.



The following example create an object of class JRadioButton.

Example

JRadioButton rd1=new JRadioButton();

Basic Methods of JRadioButton class :

Method NameDescription
void setText(String s)This method is used to set specified text on button.
String getText()It is used to return the text of 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 setEnabled(boolean b) It is used to enable or disable the button.
void addActionListener(ActionListener a) It is used to add the action listener to this object.



Example of JRadioButton with JRadioButton(String)

import javax.swing.*;

class RadioButtonEx extends JFrame
{
JRadioButton rd1,rd2;
ButtonGroup bg;
public RadioButtonEx()
{
rd1=new JRadioButton("Male");
rd2=new JRadioButton("Female");
bg=new ButtonGroup();
rd1.setBounds(50, 50, 100, 30);
rd2.setBounds(50, 80, 100, 30);
bg.add(rd1); bg.add(rd2);
add(rd1); add(rd2);
setLayout(null);
setSize(300,250);
setTitle("Radio Button");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class RadioButtonExample
{
public static void main(String[] args)
{
RadioButtonEx rb=new RadioButtonEx();
}
}



output

The code above will display :



Example of Java Swing JRadioButton with ActionListener

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class RadioButtonEx extends JFrame implements ActionListener
{
JRadioButton rd1,rd2;
ButtonGroup bg;
JButton btn;
public RadioButtonEx()
{
rd1=new JRadioButton("Married");
rd2=new JRadioButton("Unmarried");
bg=new ButtonGroup();
btn=new JButton("Click");
rd1.setBounds(50, 50, 100, 30);
rd2.setBounds(50, 80, 100, 30);
btn.setBounds(50, 120, 150, 30);
bg.add(rd1); bg.add(rd2);
add(rd1); add(rd2);
add(btn);
btn.addActionListener(this);
setLayout(null);
setSize(300,250);
setTitle("Radio Button");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public  void actionPerformed(ActionEvent ae)
{
if(rd1.isSelected())
{
JOptionPane.showMessageDialog(this,"You have selected Married");
}
if(rd2.isSelected())
{
JOptionPane.showMessageDialog(this,"You have selected Unmarried");
}
}
}
public class RadioButtonExample
{
public static void main(String[] args)
{
RadioButtonEx rb=new RadioButtonEx();
}
}



Output

The code above will display :