Java Swing Tutorial - Java Swing JRadioButton








A JRadioButton has two states: selected and unselected. A group of JRadioButtons is used when we need user to make single choice.

We can use a combination of an Action object, a string label, an icon, and a boolean flag to indicate if it is selected by default to create JRadioButton.

Create JRadioButton with no label and no image

JRadioButton rb1  = new JRadioButton();

Create JRadioButton with text as "My Choice"

JRadioButton rb2  = new JRadioButton("My Choice");

Create JRadioButton with text as "My Choice" and selected by default

JRadioButton rb3  = new JRadioButton("My Choice", true);

To select/unselect a JRadioButton, call the setSelected() methods.

To check if it is selected, use their isSelected() methods.

The following code shows how to use these methods:

tb3.setSelected(true);      // Select tb3
boolean b1  = tb3.isSelected(); 
// will store true in b1 
tb3.setSelected(false);    // Unselect tb3
boolean b2  = tb3.isSelected(); // will store false in b2

If the selection is mutually exclusive, we must group all your choices in a button group. An instance of the ButtonGroup class represents a button group.

We can add and remove a JRadioButton to a button group by using its add() and remove() methods, respectively.

Initially all members of a button group are unselected.

To form a button group, we need to add all mutually exclusive choice components to an object of the ButtonGroup class. we do not add a ButtonGroup object to a container.

We must add all choice components to the container.

The following code shows a group of three mutually exclusive JRadioButtons.

import java.awt.BorderLayout;
import java.awt.Container;
/*  w ww.  j  a va 2 s .  c  o  m*/
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class Main extends JFrame {
  ButtonGroup genderGroup = new ButtonGroup();
  JRadioButton genderMale = new JRadioButton("Male");
  JRadioButton genderFemale = new JRadioButton("Female");
  JRadioButton genderUnknown = new JRadioButton("Unknown");

  public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    genderGroup.add(genderMale);
    genderGroup.add(genderFemale);
    genderGroup.add(genderUnknown);

    Box b1 = Box.createVerticalBox();
    b1.add(genderMale);
    b1.add(genderFemale);
    b1.add(genderUnknown);

    Container contentPane = this.getContentPane();
    contentPane.add(b1, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    Main bf = new Main();
    bf.pack();
    bf.setVisible(true);
  }
}




Action Listener

The following code shows how to listen to JRadioButton Events with an ActionListener.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// ww w .j  av  a  2s.  co m
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Grouping Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ActionListener sliceActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton aButton = (AbstractButton) actionEvent.getSource();
        System.out.println("Selected: " + aButton.getText());
      }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addActionListener(sliceActionListener);
    bRadioButton.addActionListener(sliceActionListener);


    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}




Change Listener

The following code shows how to listen to JRadioButton Events with a ChangeListener.

It tells you when the selected radio button is armed, pressed, selected, or released.

import java.awt.GridLayout;
//w w  w . ja  v  a 2s .c  o m
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Grouping Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changEvent) {
        AbstractButton aButton = (AbstractButton)changEvent.getSource();
        ButtonModel aModel = aButton.getModel();
        boolean armed = aModel.isArmed();
        boolean pressed = aModel.isPressed();
        boolean selected = aModel.isSelected();
        System.out.println("Changed: " + armed + "/" + pressed + "/" +
          selected);
      }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addChangeListener(changeListener);
    bRadioButton.addChangeListener(changeListener);

    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
  
}

Item Listener

The following code shows how to listen to JRadioButton Events with an ItemListener.

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
//  w  ww . j a  v  a2  s . co  m
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Grouping Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ItemListener itemListener = new ItemListener() {
      String lastSelected;
      public void itemStateChanged(ItemEvent itemEvent) {
        AbstractButton aButton = (AbstractButton)itemEvent.getSource();
        int state = itemEvent.getStateChange();
        String label = aButton.getText();
        String msgStart;
        if (state == ItemEvent.SELECTED) {
          if (label.equals(lastSelected)) {
            msgStart = "Reselected -> ";
          }  else {
           msgStart = "Selected -> ";
          }
          lastSelected = label;
        }  else {
          msgStart = "Deselected -> ";
        }
        System.out.println(msgStart + label);
      }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addItemListener(itemListener);
    bRadioButton.addItemListener(itemListener);

    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
  
}