Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Create a JRadioButton Component in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Create a JRadioButton Component in Java

Advertisement
In this section, you will learn how to create a radio button in java swing.

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:

ButtonGroup:
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.

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

setSelected():
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");
  buttonGroup.add(Male);
  panel.add(Male);
  Female = new JRadioButton("Female");
  buttonGroup.add(Female);
  panel.add(Female);
  Male.setSelected(true);
  frame.add(panel);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Discuss: Create a JRadioButton Component in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Shrikant Badiger
June 29, 2011
good

hi this is nice example to understand the radio button functionality thanks and regards shrikant badiger
Pink tree
November 13, 2011
How to add actionListener to RadioButton

For Example, JButtonGroup g=new JButtonGroup(); JRadioButton DecimalConvert=new JRadioButton("Decimal Converting"); g.add(DecimalConvert); DecimalConvert.addActionListener(this); I made a program to convert Decimal to binari and hexa and octal also each of them to another . I create 4 RadioButtons but I Don't know how to add Actions to them if the user choss Octal do Octal converting Thanx for Reading My Comment , Appreciate it
Fed
February 15, 2012
radio

if(textField.getText().isEmpty() || textField_1.getText().isEmpty() || textField_2.getText().isEmpty() ){ JOptionPane.showMessageDialog(frame,"Podaj wszystkie wymiary!!!"); } else { if (rbt1.isSelected()){ if (textField_3.getText().isEmpty()){ JOptionPane.showMessageDialog(frame,"Podaj cene Plytek!!!"); } else{ int wys = Integer.parseInt(textField.getText()); int szer = Integer.parseInt(textField_1.getText()); int dl = Integer.parseInt(textField_2.getText()); int cena = Integer.parseInt(textField_3.getText()); int okna = Integer.parseInt(textField_8.getText()); float powierzchnia; float koszt; powierzchnia = (dl*szer + 2*szer*wys + 2*dl*wys) - okna; koszt = powierzchnia * cena; textField_6.setText("" +powierzchnia); textField_7.setText("" + koszt); } } if (rbt2.isSelected()){ if (textField_4.getText().isEmpty()){ JOptionPane.showMessageDialog(frame,"Podaj cene Paneli!!!"); } else{ int wys = Integer.parseInt(textField.getText()); int szer = Integer.parseInt(textField_1.getText()); int dl = Integer.parseInt(textField_2.getText()); int cena = Integer.parseInt(textField_4.getText()); int okna = Integer.parseInt(textField_8.getText()); float powierzchnia; float koszt; powierzchnia = (dl*szer + 2*szer*wys + 2*dl*wys) - okna; koszt = powierzchnia * cena; textField_6.setText("" +powierzchnia); textField_7.setText("" + koszt); } } if (rbt3.isSelected()){ if (textField_5.getText().isEmpty()){ JOptionPane.showMessageDialog(frame,"Podaj cene Farby!!!"); } else{ int wys = Integer.parseInt(textField.getText()); int szer = Integer.parseInt(textField_1.getText()); int dl = Integer.parseInt(textField_2.getText()); int cena = Integer.parseInt(textField_5.getText()); int okna = Integer.parseInt(textField_8.getText()); float powierzchnia; float koszt; powierzchnia = (dl*szer + 2*szer*wys + 2*dl*wys) - okna; koszt = powierzchnia * cena; textField_6.setText("" +powierzchnia); textField_7.setText("" + koszt); } } } } });
ravindra
April 4, 2012
java

pleas insert the so many example programs coding a radio buttons.
Pavan
April 4, 2012
java

thanks
Alex Leong
October 16, 2012
Remove selected radio button or check box

how to remove the selected radio button?
DMCA.com