Class FlatTriStateCheckBox

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants
Direct Known Subclasses:
FlatThemePreviewSwitches.ZoomTriStateCheckBox

public class FlatTriStateCheckBox extends JCheckBox
A tri-state check box.

The initial state is FlatTriStateCheckBox.State.INDETERMINATE.

By default, the third state is allowed and clicking on the checkbox cycles through all three states. If you want that the user can cycle only through two states, disallow intermediate state using setAllowIndeterminate(boolean). Then you can still set the indeterminate state via API if necessary, but the user can not.

The default state cycle order is FlatTriStateCheckBox.State.UNSELECTED to FlatTriStateCheckBox.State.INDETERMINATE to FlatTriStateCheckBox.State.SELECTED. This is the same order as used by macOS, win32, IntelliJ IDEA and on the web as recommended by W3C in Tri-State Checkbox Example).

If isAltStateCycleOrder() returns true, the state cycle order is FlatTriStateCheckBox.State.UNSELECTED to FlatTriStateCheckBox.State.SELECTED to FlatTriStateCheckBox.State.INDETERMINATE. This order is used by Windows 10 UWP apps.

If you prefer the alternative state cycle order for all tri-state check boxes, enable it using:

 UIManager.put( "FlatTriStateCheckBox.altStateCycleOrder", true );
 

To display the third state, this component requires an LaF that supports painting the indeterminate state if client property "JButton.selectedState" has the value "indeterminate". FlatLaf and macOS Aqua LaF support the third state. For other LaFs a magenta rectangle is painted around the component for the third state.

See Also:
  • Field Details

    • state

    • allowIndeterminate

      private boolean allowIndeterminate
    • altStateCycleOrder

      private boolean altStateCycleOrder
  • Constructor Details

    • FlatTriStateCheckBox

      public FlatTriStateCheckBox()
    • FlatTriStateCheckBox

      public FlatTriStateCheckBox(String text)
    • FlatTriStateCheckBox

      public FlatTriStateCheckBox(String text, FlatTriStateCheckBox.State initialState)
  • Method Details

    • getState

      public FlatTriStateCheckBox.State getState()
      Returns the state as FlatTriStateCheckBox.State enum.

      Alternatively you can use getChecked() to get all three states as Boolean or isIndeterminate() to check only for indeterminate state.

    • setState

      public void setState(FlatTriStateCheckBox.State state)
      Sets the state as FlatTriStateCheckBox.State enum.
    • nextState

      Returns the next state that follows the given state, depending on isAllowIndeterminate() and isAltStateCycleOrder().
    • getChecked

      public Boolean getChecked()
      Returns the state as Boolean. Returns null if the state is FlatTriStateCheckBox.State.INDETERMINATE.

      Alternatively you can use getState() to get state as FlatTriStateCheckBox.State enum or isIndeterminate() to check only for indeterminate state.

    • setChecked

      public void setChecked(Boolean value)
      Sets the state as Boolean. Passing null sets state to FlatTriStateCheckBox.State.INDETERMINATE.
    • setSelected

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

      public boolean isIndeterminate()
      Returns whether state is indeterminate.
    • setIndeterminate

      public void setIndeterminate(boolean indeterminate)
      Sets indeterminate state.
    • isAllowIndeterminate

      public boolean isAllowIndeterminate()
      Returns whether indeterminate state is allowed.

      This affects only the user when clicking on the checkbox. Setting state to indeterminate via API is always allowed.

    • setAllowIndeterminate

      public void setAllowIndeterminate(boolean allowIndeterminate)
      Sets whether indeterminate state is allowed.

      This affects only the user when clicking on the checkbox. Setting state to indeterminate via API is always allowed.

    • isAltStateCycleOrder

      public boolean isAltStateCycleOrder()
      Returns whether alternative state cycle order should be used.
    • setAltStateCycleOrder

      public void setAltStateCycleOrder(boolean altStateCycleOrder)
      Sets whether alternative state cycle order should be used.
    • paintComponent

      protected void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • paintIndeterminateState

      protected void paintIndeterminateState(Graphics g)
      Paints the indeterminate state if the current LaF does not support displaying the indeterminate state. The default implementation draws a magenta rectangle around the component.
    • isIndeterminateStateSupported

      protected boolean isIndeterminateStateSupported()
      Returns whether the current LaF supports displaying the indeterminate state. Returns true for FlatLaf and macOS Aqua.