com.jgoodies.binding.adapter
public final class RadioButtonAdapter extends javax.swing.JToggleButton.ToggleButtonModel
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.PROPERTYNAME_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.PROPERTYNAME_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.PROPERTYNAME_ORIENTATION); JRadioButton landscapeButton = new JRadioButton("Landscape"); landscapeButton.setModel(new RadioButtonAdapter(model, "landscape"); JRadioButton portraitButton = new JRadioButton("Portrait"); portraitButton.setModel(new RadioButtonAdapter(model, "portrait");
ButtonModel
,
JRadioButton
,
JRadioButtonMenuItem
,
Serialized FormConstructor and Description |
---|
RadioButtonAdapter(ValueModel subject,
java.lang.Object choice)
Constructs a RadioButtonAdapter on the given subject ValueModel
for the specified choice.
|
Modifier and Type | Method and Description |
---|---|
void |
setGroup(javax.swing.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 . |
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
public RadioButtonAdapter(ValueModel subject, java.lang.Object choice)
choice
.subject
- the subject that holds the valuechoice
- the choice that indicates that this adapter is selectedjava.lang.NullPointerException
- if the subject is null
public void setSelected(boolean b)
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.
setSelected
in interface javax.swing.ButtonModel
setSelected
in class javax.swing.JToggleButton.ToggleButtonModel
b
- true
sets the choice value as subject value,
and is intended to select this adapter (although it may not happen);
false
does nothingpublic void setGroup(javax.swing.ButtonGroup group)
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.setGroup
in interface javax.swing.ButtonModel
setGroup
in class javax.swing.DefaultButtonModel
group
- the ButtonGroup
that will be rejectedjava.lang.UnsupportedOperationException
- if the group is not null
.Copyright © 2002-2010 JGoodies Karsten Lentzsch. All Rights Reserved.