com.jgoodies.binding.adapter
Class RadioButtonAdapter

java.lang.Object
  extended by javax.swing.DefaultButtonModel
      extended by javax.swing.JToggleButton.ToggleButtonModel
          extended by com.jgoodies.binding.adapter.RadioButtonAdapter
All Implemented Interfaces:
ItemSelectable, Serializable, ButtonModel

public final class RadioButtonAdapter
extends JToggleButton.ToggleButtonModel

Converts ValueModels to the ToggleButtonModel interface. Useful to bind JRadioButtons and JRadioButtonMenuItems to a ValueModel.

This adapter holds a choice object that is used to determine the selection state if the underlying subject ValueModel changes its value. This model is selected if the subject's value equals the choice object. And if the selection is set, the choice object is set to the subject.

Note: You must not use a ButtonGroup with this adapter. The RadioButtonAdapter ensures that only one choice is selected by sharing a single subject ValueModel - at least if all choice values differ. See also the example below.

Example:

 // Recommended binding style using a factory
 PresentationModel presentationModel = new PresentationModel(printerSettings);
 ValueModel orientationModel =
     presentationModel.getModel(PrinterSettings.PROPERTY_ORIENTATION);
 JRadioButton landscapeButton = BasicComponentFactory.createRadioButton(
     orientationModel, PrinterSettings.LANDSCAPE, "Landscape");
 JRadioButton portraitButton  = BasicComponentFactory.createRadioButton(
     orientationModel, PrinterSettings.PORTRAIT, "Portrait");

 // Binding using the Bindings class
 ValueModel orientationModel =
     presentationModel.getModel(PrinterSettings.PROPERTY_ORIENTATION);
 JRadioButton landscapeButton = new JRadioButton("Landscape");
 Bindings.bind(landscapeButton, orientationModel, "landscape");

 JRadioButton portraitButton = new JRadioButton("Portrait");
 Bindings.bind(portraitButton, orientationModel, "portrait");

 // Hand-made style
 ValueModel orientationModel =
     presentationModel.getModel(PrinterSettings.PROPERTY_ORIENTATION);
 JRadioButton landscapeButton = new JRadioButton("Landscape");
 landscapeButton.setModel(new RadioButtonAdapter(model, "landscape");

 JRadioButton portraitButton = new JRadioButton("Portrait");
 portraitButton.setModel(new RadioButtonAdapter(model, "portrait");
 

Version:
$Revision: 1.11 $
Author:
Karsten Lentzsch
See Also:
ButtonModel, JRadioButton, JRadioButtonMenuItem, Serialized Form

Field Summary
 
Fields inherited from class javax.swing.DefaultButtonModel
actionCommand, ARMED, changeEvent, ENABLED, group, listenerList, mnemonic, PRESSED, ROLLOVER, SELECTED, stateMask
 
Constructor Summary
RadioButtonAdapter(ValueModel subject, Object choice)
          Constructs a RadioButtonAdapter on the given subject ValueModel for the specified choice.
 
Method Summary
 void setGroup(ButtonGroup group)
          Throws an UnsupportedOperationException if the group is not null.
 void setSelected(boolean b)
          First, the subject value is set to this adapter's choice value if the argument is true.
 
Methods inherited from class javax.swing.JToggleButton.ToggleButtonModel
isSelected, setPressed
 
Methods inherited from class javax.swing.DefaultButtonModel
addActionListener, addChangeListener, addItemListener, fireActionPerformed, fireItemStateChanged, fireStateChanged, getActionCommand, getActionListeners, getChangeListeners, getGroup, getItemListeners, getListeners, getMnemonic, getSelectedObjects, isArmed, isEnabled, isPressed, isRollover, removeActionListener, removeChangeListener, removeItemListener, setActionCommand, setArmed, setEnabled, setMnemonic, setRollover
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RadioButtonAdapter

public RadioButtonAdapter(ValueModel subject,
                          Object choice)
Constructs a RadioButtonAdapter on the given subject ValueModel for the specified choice. The created adapter will be selected if and only if the subject's initial value equals the given choice.

Parameters:
subject - the subject that holds the value
choice - the choice that indicates that this adapter is selected
Throws:
NullPointerException - if the subject is null
Method Detail

setSelected

public void setSelected(boolean b)
First, the subject value is set to this adapter's choice value if the argument is true. Second, this adapter's state is set to the then current subject value. The latter ensures that the selection state is synchronized with the subject - even if the subject rejects the change.

Does nothing if the boolean argument is false, or if this adapter is already selected.

Specified by:
setSelected in interface ButtonModel
Overrides:
setSelected in class JToggleButton.ToggleButtonModel
Parameters:
b - true sets the choice value as subject value, and is intended to select this adapter (although it may not happen); false does nothing

setGroup

public void setGroup(ButtonGroup group)
Throws an UnsupportedOperationException if the group is not null. You need not and must not use a ButtonGroup with a set of RadioButtonAdapters. RadioButtonAdapters form a group by sharing the same subject ValueModel.

Specified by:
setGroup in interface ButtonModel
Overrides:
setGroup in class DefaultButtonModel
Parameters:
group - the ButtonGroup that will be rejected
Throws:
UnsupportedOperationException - if the group is not null.


Copyright © 2002-2013 JGoodies Software GmbH. All Rights Reserved.