Class TristateCheckBox

All Implemented Interfaces:
ActionListener, ImageObserver, ItemSelectable, MenuContainer, Serializable, EventListener, Accessible, SwingConstants
Direct Known Subclasses:
NullTristateCheckBox

public class TristateCheckBox extends JCheckBox implements ActionListener
TristateCheckBox is a check box with three states - selected, unselected and mixed (a.k.a partial selected state). Internally it uses a new class called TristateButtonModel to store the 3rd mixed state information.

The mixed state uses a different check icon. Instead of a checked sign in the selected state as in a regular check box, we use a square sign to indicate the mixed state. On different L&Fs, it might look different. TristateCheckBox supports most of the standard L&Fs such as Windows L&F, Metal L&F, Motif L&F, Nimbus L&F, Aqua L&F etc. For most L&Fs, we use a new UIDefault "TristateCheckBox.icon" to paint in three different states. However for Aqua L&F, we actually leveraged a client property provided by Apple to display the icon for the mixed state (refer to Radar #8930094 at http://developer.apple.com/library/mac/#releasenotes/Java/JavaSnowLeopardUpdate4LeopardUpdate9RN/ResolvedIssues/ResolvedIssues.html). To make it extensible for other L&Fs who might provide a built-in mixed state for check box, we support two types of customizations.

 
  • using client property as Aqua. You can define your own client properties and use UIDefaults to tell us how to set it. For example:
  • "TristateCheckBox.icon", null, "TristateCheckBox.setMixed.clientProperty", new Object[]{"JButton.selectedState", "indeterminate"}, "TristateCheckBox.clearMixed.clientProperty", new Object[]{"JButton.selectedState", null},
using component name. Some Synth-based L&Fs use component name to define style. If so, you can use the following two UIDefaults. For example: "TristateCheckBox.setMixed.componentName", "HalfSelected", "TristateCheckBox.clearMixed.componentName", "",
The correct listener for state change is ActionListener. It will be fired when the state is changed. The ItemListener is only fired when changing from selected state to unselected state or vice versa. Only ActionListener will be fired for all three states.
See Also:
  • Field Details

  • Constructor Details

    • TristateCheckBox

      public TristateCheckBox(String text, Icon icon)
    • TristateCheckBox

      public TristateCheckBox(String text)
    • TristateCheckBox

      public TristateCheckBox()
  • Method Details

    • init

      protected void init(String text, Icon icon)
      Overrides:
      init in class AbstractButton
    • createButtonModel

      protected ButtonModel createButtonModel()
      Creates the button model. In this case, it is always a TristateButtonModel.
      Returns:
      TristateButtonModel
    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JCheckBox
    • adjustMixedIcon

      protected void adjustMixedIcon()
    • restoreMixedIcon

      protected void restoreMixedIcon()
    • isMixed

      public boolean isMixed()
      Checks if the check box is in mixed selection state.
      Returns:
      true or false.
    • setMixed

      public void setMixed(boolean b)
      Sets the check box to mixed selection state.
      Parameters:
      b - true or false. True means mixed state. False means unselected state.
    • getState

      public int getState()
      Gets the selection state. It could be one of the three states as defined - STATE_SELECTED, STATE_UNSELECTED and STATE_MIXED.
      Returns:
      one of the three selection states.
    • setSelected

      public void setSelected(boolean b)
      Overrides:
      setSelected in class AbstractButton
    • setState

      public void setState(int state)
      Sets the selection state. It could be one of the three states as defined - STATE_SELECTED, STATE_UNSELECTED and STATE_MIXED.
      Parameters:
      state - one of the three selection states.
    • actionPerformed

      public void actionPerformed(ActionEvent e)
      Specified by:
      actionPerformed in interface ActionListener
    • stateUpdated

      protected void stateUpdated(int state)
      This method is called when the selection state changes.
      Parameters:
      state - the new selection state.