com.jgoodies.binding.adapter
Class ToggleButtonAdapter

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

public final class ToggleButtonAdapter
extends javax.swing.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.11 $
Author:
Karsten Lentzsch
See Also:
ButtonModel, JCheckBox, JCheckBoxMenuItem, Serialized Form

Field Summary
 
Fields inherited from class javax.swing.DefaultButtonModel
actionCommand, ARMED, changeEvent, ENABLED, group, listenerList, mnemonic, PRESSED, ROLLOVER, SELECTED, stateMask
 
Constructor Summary
ToggleButtonAdapter(ValueModel subject)
          Constructs a ToggleButtonAdapter on the given subject ValueModel.
ToggleButtonAdapter(ValueModel subject, java.lang.Object selectedValue, java.lang.Object deselectedValue)
          Constructs a ToggleButtonAdapter on the given subject ValueModel using the specified values for the selected and deselected state.
 
Method Summary
 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.
 
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, setGroup, setMnemonic, setRollover
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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
Throws:
java.lang.NullPointerException - if the subject is null.

ToggleButtonAdapter

public ToggleButtonAdapter(ValueModel subject,
                           java.lang.Object selectedValue,
                           java.lang.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
Throws:
java.lang.NullPointerException - if the subject is null.
java.lang.IllegalArgumentException - if the selected and deselected values are equal
Method Detail

setSelected

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

Specified by:
setSelected in interface javax.swing.ButtonModel
Overrides:
setSelected in class javax.swing.JToggleButton.ToggleButtonModel
Parameters:
b - true sets the selected value as subject value, false sets the deselected value as subject value


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