Class JXRadioGroup<T>

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

@JavaBean public class JXRadioGroup<T> extends JPanel

JXRadioGroup is a group of radio buttons that functions as a unit. It is similar in concept to a JComboBox in functionality, but can offer a better presentation for a small number of choices. JXRadioGroup should be used in preference to JComboBox when the number of choices is small (less than six) or the choices are verbose.

Notes:

  1. Enabling and disabling the JXRadioGroup will enable/disable all of the child buttons inside the JXRadioGroup.
  2. If the generic type parameter of JXRadioGroup is a subclass of AbstractButton, then the buttons will be added "as is" to the container. If the generic type is anything else, buttons will be created as JRadioButton objects, and the button text will be set by calling toString() on the value object.
  3. Alternatively, if you want to configure the buttons individually, construct the JXRadioGroup normally, and then call getChildButton(int) or getChildButton(Object) and configure the buttons.

TODO back with a model (possibly reuse of extend ComboBoxModel

Version:
1.0
Author:
Amy Fowler, Noel Grandin
See Also:
  • Constructor Details

    • JXRadioGroup

      public JXRadioGroup()
      Create a default JXRadioGroup with a default layout axis of BoxLayout.X_AXIS.
    • JXRadioGroup

      public JXRadioGroup(T[] radioValues)
      Create a default JXRadioGroup with a default layout axis of BoxLayout.X_AXIS.
      Parameters:
      radioValues - the list of values used to create the group.
  • Method Details

    • create

      public static <T> JXRadioGroup<T> create(T[] radioValues)
      Convenience factory method. Reduces code clutter when dealing with generics.
      Parameters:
      radioValues - the list of values used to create the group.
    • setLayoutAxis

      public void setLayoutAxis(int axis)
      Set the layout axis of the radio group.
      Parameters:
      axis - values from BoxLayout.
    • setValues

      public void setValues(T[] radioValues)
      Sets the values backing this group. This replaces the current set of values with the new set.
      Parameters:
      radioValues - the new backing values for this group
    • add

      public void add(T radioValue)
      You can use this method to manually add your own AbstractButton objects, provided you declared the class as JXRadioGroup<JRadioButton>.
    • getSelectedButton

      public AbstractButton getSelectedButton()
      Gets the currently selected button.
      Returns:
      the currently selected button
      See Also:
    • getSelectedValue

      public T getSelectedValue()
      The currently selected value.
      Returns:
      the current value
    • setSelectedValue

      public void setSelectedValue(T value)
      Selects the supplied value.
      Parameters:
      value - the value to select
    • getChildButton

      public AbstractButton getChildButton(int index)
      Retrieve the child button by index.
    • getChildButton

      public AbstractButton getChildButton(T value)
      Retrieve the child button that represents this value.
    • getChildButtonCount

      public int getChildButtonCount()
      Get the number of child buttons.
    • addActionListener

      public void addActionListener(ActionListener l)
      Adds an ActionListener.

      The ActionListener will receive an ActionEvent when a selection has been made.

      Parameters:
      l - the ActionListener that is to be notified
      See Also:
    • removeActionListener

      public void removeActionListener(ActionListener l)
      Removes an ActionListener.
      Parameters:
      l - the ActionListener to remove
    • getActionListeners

      public ActionListener[] getActionListeners()
      Returns an array of all the ActionListeners added to this JRadioGroup with addActionListener().
      Returns:
      all of the ActionListeners added or an empty array if no listeners have been added
    • fireActionEvent

      protected void fireActionEvent(ActionEvent e)
      Notifies all listeners that have registered interest for notification on this event type.
      Parameters:
      e - the event to pass to the listeners
      See Also:
    • setEnabled

      public void setEnabled(boolean enabled)
      Enable/disable all of the child buttons
      Overrides:
      setEnabled in class JComponent
      See Also: