Class RadioButton<T extends RadioButton<T>>


public class RadioButton<T extends RadioButton<T>> extends Input<T>
HTML radio button input element for mutually exclusive selection.

This class extends Input to provide a radio button control where only one option can be selected within a group of radio buttons sharing the same name. The class manages group relationships automatically and ensures only one button per group can be checked at a time.

Features:

  • Mutually exclusive selection within named groups
  • Automatic group management and coordination
  • Programmatic check/uncheck control
  • Selected value retrieval for the group
  • Weak reference management for memory efficiency
  • Automatic cleanup on page removal

Usage:

 RadioButton opt1 = new RadioButton("size", "small");
 RadioButton opt2 = new RadioButton("size", "medium");
 RadioButton opt3 = new RadioButton("size", "large");
 opt2.check();  // Only opt2 will be checked
 Object selected = opt1.getSelectedValue();  // Returns "medium"
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • RadioButton

      public RadioButton()
      Creates a new RadioButton with no name or value.
    • RadioButton

      public RadioButton(String name, Object value)
      Creates a new RadioButton with the specified group name and value.
      Parameters:
      name - The name of the radio button group.
      value - The value submitted when this radio button is selected.
    • RadioButton

      public RadioButton(String name, String value)
      Creates a new RadioButton with the specified group name and string value.
      Parameters:
      name - The name of the radio button group.
      value - The string value submitted when this radio button is selected.
  • Method Details

    • setChecked

      public final T setChecked(boolean checked)
      Sets the checked state of this radio button.
      Overrides:
      setChecked in class InputElement<T extends RadioButton<T>>
      Parameters:
      checked - true to check this radio button, false to uncheck it.
      Returns:
      This element for method chaining.
    • check

      public final void check()
      Checks this radio button and unchecks all other radio buttons in the same group.
    • uncheck

      public final void uncheck()
      Unchecks this radio button.
    • getSelectedValue

      public final Object getSelectedValue()
      Returns the value of the currently selected radio button in this button's group.
      Returns:
      The value of the checked radio button in the group.
    • onChecked

      public final void onChecked()
      Called by the event handler when this radio button is checked by the user.
    • isChecked

      public final boolean isChecked()
      Returns whether this radio button is currently checked.
      Returns:
      true if this radio button is checked, false otherwise.
    • onRemovedFromPage

      protected final void onRemovedFromPage()
      Description copied from class: Element
      Hook method called when this element is removed from a page.

      Override this method to perform cleanup when the element is detached.

      Overrides:
      onRemovedFromPage in class StyledElement<T extends RadioButton<T>>
    • prewrite

      protected final void prewrite()
      Description copied from class: Element
      Hook method called before the element is written to output.

      Override this method to perform any last-minute configuration before rendering.

      Overrides:
      prewrite in class Element<T extends RadioButton<T>>