Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

javax.swing.JRadioButton and Related Classes

This section describes the javax.swing.JRadioButton class, the javax.swing.ButtonGroup class, and the javax.swing.JToggleButton.ToggleButtonModel class.

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.

Last update: 2009.

Sections in This Chapter

javax.swing.JRadioButton and Related Classes

ActionListener, ChangeListener and ItemListener

getSelection() - Getting Selected Button

Dr. Herong Yang, updated in 2009
javax.swing.JRadioButton and Related Classes