Selecting a Radio Button component in Java

In this section, you will learn how to set the radio buttons in a group so that only one can be selected at a time.

Selecting a Radio Button component in Java

Selecting a Radio Button component in Java

     

In this section, you will learn how to set the radio buttons in a group so that only one can be selected at a time.

This program shows five radio buttons with labeled by "First", "Second", "Third", "Fourth" and "Fifth". This program also show a label which contains the text "Roseindia.net" but when you click on any radio button from a ButtonGroup the text of the selected radio button is shown on the label and a message box will be shown with message holds the selected radio button label. This is done through the generating event for the different-different radio buttons. Following are the screen shots for the result of the given program:

Radio Buttons without selection

Frame with a message dialog box when you select the second radio button

For this purposes, there are some APIs or methods have been used as follows:

getActionCommand():
This is the method of the ActionEvent class which returns the source title in string of the generated event.

Here is the code of the program:

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

public class SelectRadioButton{
  JLabel label;
  public static void main(String[] args){
  SelectRadioButton sr = new SelectRadioButton();
  }

  public SelectRadioButton(){
  JFrame frame = new JFrame("Radio button selection");
  JRadioButton first = new JRadioButton("First");
  JRadioButton second = new JRadioButton("Second");
  JRadioButton third = new JRadioButton("Third");
  JRadioButton fourth = new JRadioButton("Fourth");
  JRadioButton fifth = new JRadioButton("Fifth");
  JPanel panel = new JPanel();
  panel.add(first);
  panel.add(second);
  panel.add(third);
  panel.add(fourth);
  panel.add(fifth);
  ButtonGroup bg = new ButtonGroup();
  bg.add(first);
  bg.add(second);
  bg.add(third);
  bg.add(fourth);
  bg.add(fifth);
  first.addActionListener(new MyAction());
  second.addActionListener(new MyAction());
  third.addActionListener(new MyAction());
  fourth.addActionListener(new MyAction());
  fifth.addActionListener(new MyAction());
  label = new JLabel("Roseindia.net");
  frame.add(panel, BorderLayout.NORTH);
  frame.add(label, BorderLayout.CENTER);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){
  label.setText(e.getActionCommand());
  JOptionPane.showMessageDialog(null,"This is the " + e.getActionCommand() 
" radio button."
);
  }
  }
}

Download this example.

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java