Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Swing JRadioButton

Part:   1   2 

This chapter describes:

  • JRadioButton class and its related classes.
  • Different types events related radio buttons.
  • How to find the selected radio button.

JRadioButton and Related Classes

javax.swing.JRadioButton - A Swing class representing a UI radio button. Some interesting methods are:

  • JRadioButton(String) - Constructor to create a radio button with the specified string displayed next to the button.
  • addActionListener(ActionListener) - Method to add an action listener to this button to handle action events. A mouse click on this button will trigger one action event.
  • addChangeListener(ChangeListener) - Method to add a change listener to this button to handle change events. A mouse click on this button will trigger many change events.
  • addIteListener(ItemListener) - Method to add an item listener to this button to handle item events. A mouse click on this button will trigger one item event.
  • setActionCommand(String) - Method to set an action command string to this button.

javax.swing.ButtonGroup - A Swing class representing a group of buttons. If one radio button is selected in a group, all other buttons in the same group are un-selected.

  • add(AbstractButton) - Method to add a button to this button group.
  • getSelection() - Method to return the selected button in this button group as a ButtonModel object.

javax.swing.JToggleButton.ToggleButtonModel - A Swing class representing a default implementation of toggle button's data model. ToggleButtonModel is an inner class nested inside javax.swing.JToggleButton, which is a base class of JRadioButton.

  • getActionCommand() - Method to return the action command string of the associated button.

JRadioButton and Event Listeners

As you can see from the previous section, a radio button can have 3 types of event listensers: ActionListener, ChangeListener, and ItemListener. The following sample program shows you when those listeners are called, and how many times:

/**
 * JRadioButtonTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JRadioButtonTest {
   public static void main(String[] a) {
      JFrame f = new JFrame("My Radio Buttons");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ButtonGroup g = new ButtonGroup();
      MyRadioButton b1 = new MyRadioButton("On");
      g.add(b1);
      f.getContentPane().add(b1,BorderLayout.NORTH);
      MyRadioButton b2 = new MyRadioButton("Off");
      g.add(b2);
      f.getContentPane().add(b2,BorderLayout.SOUTH);
      f.pack();
      f.setVisible(true);
   }
   private static class MyRadioButton extends JRadioButton 
      implements ActionListener, ChangeListener, ItemListener  {
      static int count = 0;
      String text = null;
      public MyRadioButton(String t) {
         super(t);
         text = t;
         addActionListener(this);
         addChangeListener(this);
         addItemListener(this);
      }
      public void actionPerformed(ActionEvent e) {
         count++;
         System.out.println(count+": Action performed - "+text);
      }
      public void stateChanged(ChangeEvent e) {
         count++;
         System.out.println(count+": State changed on - "+text);
      }
      public void itemStateChanged(ItemEvent e) {
         count++;
         System.out.println(count+": Item state changed - "+text);
      }
   }
}

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Swing JRadioButton