com.jgoodies.binding.adapter
Class ToggleButtonAdapter
JToggleButton.ToggleButtonModel
com.jgoodies.binding.adapter.ToggleButtonAdapter
public final class ToggleButtonAdapter
extends JToggleButton.ToggleButtonModel
Converts ValueModels to the ToggleButtonModel interface. Useful to bind
JToggleButton, JCheckBox and JCheckBoxMenuItem to a ValueModel.
This adapter holds two values that represent the selected and the deselected
state. These are used to determine the selection state if the underlying
subject ValueModel changes its value. If the selection is set, the
corresponding representant is written to the underlying ValueModel.
Constraints: The subject ValueModel must allow
read-access to its value. Also, it is strongly recommended (though not
required) that the underlying ValueModel provides only two values, for
example Boolean.TRUE and Boolean.FALSE. This is so because the toggle button
component may behave "strangely" when it is used with ValueModels that
provide more than two elements.
Examples:
// Recommended binding style using a factory
ValueModel model = presentationModel.getModel(MyBean.PROPERTYNAME_VISIBLE);
JCheckBox visibleBox = BasicComponentFactory.createCheckBox(model, "Visible");
// Binding using the Bindings class
ValueModel model = presentationModel.getModel(MyBean.PROPERTYNAME_VISIBLE);
JCheckBox visibleBox = new JCheckBox("Visible");
Bindings.bind(visibleBox, model);
// Hand-made binding
ValueModel model = presentationModel.getModel(MyBean.PROPERTYNAME_VISIBLE);
JCheckBox visibleBox = new JCheckBox("Visible");
visibleBox.setModel(new ToggleButtonAdapter(model));
javax.swing.ButtonModel
, javax.swing.JCheckBox
, javax.swing.JCheckBoxMenuItem
ToggleButtonAdapter(ValueModel subject) - Constructs a ToggleButtonAdapter on the given subject ValueModel.
|
ToggleButtonAdapter(ValueModel subject, Object selectedValue, Object deselectedValue) - Constructs a ToggleButtonAdapter on the given subject ValueModel
using the specified values for the selected and deselected state.
|
@Override | void setSelected(boolean b) - First, the subject value is set to this adapter's selected value if
the argument is
true , to the deselected value otherwise.
|
ToggleButtonAdapter
public ToggleButtonAdapter(ValueModel subject)
Constructs a ToggleButtonAdapter on the given subject ValueModel.
The created adapter will be selected if and only if the
subject's initial value is Boolean.TRUE
.
subject
- the subject that holds the value
ToggleButtonAdapter
public ToggleButtonAdapter(ValueModel subject,
Object selectedValue,
Object deselectedValue)
Constructs a ToggleButtonAdapter on the given subject ValueModel
using the specified values for the selected and deselected state.
The created adapter will be selected if and only if the
subject's initial value equals the given selectedValue
.
subject
- the subject that holds the valueselectedValue
- the value that will be set if this is selecteddeselectedValue
- the value that will be set if this is deselected
void setSelected
public @Override void setSelected(boolean b)
First, the subject value is set to this adapter's selected value if
the argument is true
, to the deselected value otherwise.
Second, this adapter's state is set to the then current subject value.
This ensures that the selected state is synchronized with the subject
- even if the subject rejects the change.
b
- true
sets the selected value as subject value,
false
sets the deselected value as subject value
Copyright © 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.