com.jgoodies.binding.adapter

Class 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));
 
Version:
$Revision: 1.6 $
Author:
Karsten Lentzsch
See Also:
javax.swing.ButtonModel, javax.swing.JCheckBox, javax.swing.JCheckBoxMenuItem

Constructor Summary

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.

Method Summary

@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.

Constructor Details

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.
Parameters:
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.
Parameters:
subject - the subject that holds the value
selectedValue - the value that will be set if this is selected
deselectedValue - the value that will be set if this is deselected

Method Details

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.
Parameters:
b - true sets the selected value as subject value, false sets the deselected value as subject value

Copyright © 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.