Create a JRadioButton Component in Java

In this section, you will learn how to create a radio button in java swing. Radio Button is like check box. Differences between check box and radio button are as follows:

  1. Check Boxes are separated from one to another where Radio Buttons are the different-different button like check box from a same ButtonGroup.
  2. You can checks multiple check boxes at once but this can never done in the case of radio button. You can select only one radio button at once from a group of the radio button.
  3. You can check or uncheck the check box but you can on check the radio button by clicking it once.

Here, you will see the JRadioButton component creation procedure in java with the help of this program. This example provides two radio buttons same ButtonGroup. These radio buttons represent the option for choosing male or female. Following is the image for the result of the given program:

Swing Radio Buttons

The creation of JRadioButton are completed by the following methods:

This is the class of the javax.swing.*; package, which is used to create a group of radio buttons from which you can select only one option from that group of the radio buttons. This is class is used by creating a instance of if using it’s constructor. Radio Buttons are added to the specified group using the add(JRadioButton) method of the ButtonGroup class.

This is the class has been used to create a single radio button for the application.

This method sets the value of the radio button. This method takes a boolean value either true or false. If you pass true value then the radio button will be selected otherwise the radio button is not selected.

Here is the code of program:

import javax.swing.*;
import java.awt.*;

public class CreateRadioButton{
  public static void main(String[] args) {
    CreateRadioButton r = new CreateRadioButton();

  public CreateRadioButton(){
    JRadioButton Male,Female;
    JFrame frame = new JFrame("Creating a JRadioButton Component");
    JPanel panel = new JPanel();
    ButtonGroup buttonGroup = new ButtonGroup();
    Male = new JRadioButton("Male");
    Female = new JRadioButton("Female");

One thought on “Create a JRadioButton Component in Java

  1. Hi, I do believe this is an excellent site. I stumbledupon it 😉 I’m going to revisit yet again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide other people.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s