Module MaterialFX

Class MFXSlider

All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class MFXSlider extends Control
This is the implementation of a Slider following Google's material design guidelines.

Extends Control rather than Slider, this has been made completely from scratch, the code is much more clean, documented, and implements many new features.

The thumb and the popup can be changed by setting the corresponding suppliers (the popup can also be removed by setting a null supplier or by returning null). You can also specify the extra gap between the popup and the thumb, see popupPaddingProperty(), and the popup position, see popupSideProperty().

MFXSlider can operate on decimal values too, up to two decimal places. By default it is set to 0, see decimalPrecisionProperty().

Just like the JavaFX' slider, MFXSlider has two working modes:

- DEFAULT, the thumb can be moved freely

- SNAP_TO_TICKS, the thumb always snaps to the closest tick (even if they're hidden)

Note that the snapping is ignored if the value is adjusted with the keyboard.

The properties to customize the ticks are: tickUnitProperty(), showMajorTicksProperty(), showMinorTicksProperty(), showTicksAtEdgesProperty(), minorTicksCountProperty().

Also note that by default (implemented in the skin), the major ticks have two different style classes according to their index position, "tick-even" or "tick-odd", just to add an extra customization.

The unitIncrementProperty() and alternativeUnitIncrementProperty() properties specify the value increment on arrow keys press (left/right when slider is Horizontal, up/down when slider is Vertical). The alternate unit increment is used when Shift or Ctrl are pressed too.

The keyboard behavior can be also disabled by setting enableKeyboardProperty() to false.

When you press on the slider's track the value is adjusted accordingly to where you pressed, the adjusting animation can also be disabled by setting animateOnPressProperty().

MFXSlider offers a brand new feature: the progress bar is bidirectional (can be disabled). This means that if the minimum value is negative the bar will progress on the opposite side to zero.

MFXSlider introduces three new css pseudo classes:

- ":range1", activated when the slider value is contained in any of the ranges specified in here getRanges1()

- ":range2", activated when the slider value is contained in any of the ranges specified in here getRanges2()

- ":range3", activated when the slider value is contained in any of the ranges specified in here getRanges3()

I know this may seem a strange approach, but it is much more flexible and allows for a lot more customization.

WARNING!

If you are changing the min, max, and initial value properties of the slider programmatically be sure to respect this order, setMin(...), setMax(...), setValue(...). This is needed for several reasons: min cannot be greater than max otherwise an exception is thrown; max cannot be lesser than min otherwise an exception is thrown; the slider's value never throws an exception if it is invalid but rather the value is clamped between the specified min and max values using NumberUtils.clamp(double, double, double). If you don't respect the order you'll end with an inconsistent state and most likely with a messed layout.

  • Property Details

  • Field Details

    • RANGE1_PSEUDO_CLASS

      protected final PseudoClass RANGE1_PSEUDO_CLASS
    • RANGE2_PSEUDO_CLASS

      protected final PseudoClass RANGE2_PSEUDO_CLASS
    • RANGE3_PSEUDO_CLASS

      protected final PseudoClass RANGE3_PSEUDO_CLASS
  • Constructor Details

    • MFXSlider

      public MFXSlider()
    • MFXSlider

      public MFXSlider(double initialValue)
    • MFXSlider

      public MFXSlider(double min, double max, double initialValue)
  • Method Details

    • defaultThumbSupplier

      protected void defaultThumbSupplier()
      Sets the default thumb supplier.

      It is basically a StackPane which contains two MFXFontIcons (both are circles). The innermost is the thumb and the outermost is the circle that indicates if the mouse is hover or pressed on the thumb.

      Note: since the outermost circle is larger that the thumb, the StackPane's layout bounds are set to be at most the thumb's width and height, otherwise it would cause layout and behavior issues.

      Also, both the thumb and the other circle are transparent to mouse events as the node returned by the supplier is the StackPane and this is the node that should respond to events.

    • defaultPopupSupplier

      protected void defaultPopupSupplier()
      Sets the default popup supplier.

      It is basically a VBox which contains a Label for the slider's value and a MFXFontIcon which is the caret.

      Note: The supplier should also deal with changes of popupSideProperty() as the text and the caret should be rotated and positioned accordingly.

      Also note that the so called "popup" is not really a JavaFX popup but a node (Region to be precise) because this makes handling it's position way easier (with a real popup we must deal with screen coordinates and it's a real pita).
    • getMin

      public double getMin()
      Gets the value of the property min.
      Property description:
      Specifies the minimum value the slider can reach.
    • minProperty

      public DoubleProperty minProperty()
      Specifies the minimum value the slider can reach.
      See Also:
    • setMin

      public void setMin(double min)
      Sets the value of the property min.
      Property description:
      Specifies the minimum value the slider can reach.
    • getMax

      public double getMax()
      Gets the value of the property max.
      Property description:
      Specifies the maximum value the slider can reach.
    • maxProperty

      public DoubleProperty maxProperty()
      Specifies the maximum value the slider can reach.
      See Also:
    • setMax

      public void setMax(double max)
      Sets the value of the property max.
      Property description:
      Specifies the maximum value the slider can reach.
    • getValue

      public double getValue()
      Gets the value of the property value.
      Property description:
      Specifies the slider's actual value.
    • valueProperty

      public DoubleProperty valueProperty()
      Specifies the slider's actual value.
      See Also:
    • setValue

      public void setValue(double value)
      Sets the value of the property value.
      Property description:
      Specifies the slider's actual value.
    • getThumbSupplier

      public Supplier<Node> getThumbSupplier()
      Gets the value of the property thumbSupplier.
      Property description:
      Specifies the supplier used to build the slider's thumb.

      Attempting to set or return a null value will fallback to the defaultThumbSupplier().

    • thumbSupplierProperty

      public SupplierProperty<Node> thumbSupplierProperty()
      Specifies the supplier used to build the slider's thumb.

      Attempting to set or return a null value will fallback to the defaultThumbSupplier().

      See Also:
    • setThumbSupplier

      public void setThumbSupplier(Supplier<Node> thumbSupplier)
      Sets the value of the property thumbSupplier.
      Property description:
      Specifies the supplier used to build the slider's thumb.

      Attempting to set or return a null value will fallback to the defaultThumbSupplier().

    • getPopupSupplier

      public Supplier<Region> getPopupSupplier()
      Gets the value of the property popupSupplier.
      Property description:
      Specifies the supplier used to build the slider's popup.

      You can also set or return null to remove the popup.

    • popupSupplierProperty

      public SupplierProperty<Region> popupSupplierProperty()
      Specifies the supplier used to build the slider's popup.

      You can also set or return null to remove the popup.

      See Also:
    • setPopupSupplier

      public void setPopupSupplier(Supplier<Region> popupSupplier)
      Sets the value of the property popupSupplier.
      Property description:
      Specifies the supplier used to build the slider's popup.

      You can also set or return null to remove the popup.

    • getPopupPadding

      public double getPopupPadding()
      Gets the value of the property popupPadding.
      Property description:
      Specifies the extra gap between the thumb and the popup.
    • popupPaddingProperty

      public DoubleProperty popupPaddingProperty()
      Specifies the extra gap between the thumb and the popup.
      See Also:
    • setPopupPadding

      public void setPopupPadding(double popupPadding)
      Sets the value of the property popupPadding.
      Property description:
      Specifies the extra gap between the thumb and the popup.
    • getDecimalPrecision

      public int getDecimalPrecision()
      Gets the value of the property decimalPrecision.
      Property description:
      Specifies the number of decimal places for the slider's value.
    • decimalPrecisionProperty

      public IntegerProperty decimalPrecisionProperty()
      Specifies the number of decimal places for the slider's value.
      See Also:
    • setDecimalPrecision

      public void setDecimalPrecision(int decimalPrecision)
      Sets the value of the property decimalPrecision.
      Property description:
      Specifies the number of decimal places for the slider's value.
    • isEnableKeyboard

      public boolean isEnableKeyboard()
      Gets the value of the property enableKeyboard.
      Property description:
      Specifies if the value can be adjusted with the keyboard or not.
    • enableKeyboardProperty

      public BooleanProperty enableKeyboardProperty()
      Specifies if the value can be adjusted with the keyboard or not.
      See Also:
    • setEnableKeyboard

      public void setEnableKeyboard(boolean enableKeyboard)
      Sets the value of the property enableKeyboard.
      Property description:
      Specifies if the value can be adjusted with the keyboard or not.
    • getRanges1

      public ObservableList<NumberRange<Double>> getRanges1()
      Returns the first list of ranges.
    • getRanges2

      public ObservableList<NumberRange<Double>> getRanges2()
      Returns the second list of ranges.
    • getRanges3

      public ObservableList<NumberRange<Double>> getRanges3()
      Returns the third list of ranges.
    • getSliderMode

      public SliderEnums.SliderMode getSliderMode()
      Gets the value of the property sliderMode.
      Property description:
      Specifies the slider mode. Can be DEFAULT (freely adjust the thumb) or SNAP_TO_TICKS (the thumb will always snap to ticks).
    • sliderModeProperty

      public StyleableObjectProperty<SliderEnums.SliderMode> sliderModeProperty()
      Specifies the slider mode. Can be DEFAULT (freely adjust the thumb) or SNAP_TO_TICKS (the thumb will always snap to ticks).
      See Also:
    • setSliderMode

      public void setSliderMode(SliderEnums.SliderMode sliderMode)
      Sets the value of the property sliderMode.
      Property description:
      Specifies the slider mode. Can be DEFAULT (freely adjust the thumb) or SNAP_TO_TICKS (the thumb will always snap to ticks).
    • getUnitIncrement

      public double getUnitIncrement()
      Gets the value of the property unitIncrement.
      Property description:
      Specifies the value to add/subtract to the slider's value when an arrow key is pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

    • unitIncrementProperty

      public StyleableDoubleProperty unitIncrementProperty()
      Specifies the value to add/subtract to the slider's value when an arrow key is pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

      See Also:
    • setUnitIncrement

      public void setUnitIncrement(double unitIncrement)
      Sets the value of the property unitIncrement.
      Property description:
      Specifies the value to add/subtract to the slider's value when an arrow key is pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

    • getAlternativeUnitIncrement

      public double getAlternativeUnitIncrement()
      Gets the value of the property alternativeUnitIncrement.
      Property description:
      Specifies the value to add/subtract to the slider's value when an arrow key and Shift or Ctrl are pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

    • alternativeUnitIncrementProperty

      public StyleableDoubleProperty alternativeUnitIncrementProperty()
      Specifies the value to add/subtract to the slider's value when an arrow key and Shift or Ctrl are pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

      See Also:
    • setAlternativeUnitIncrement

      public void setAlternativeUnitIncrement(double alternativeUnitIncrement)
      Sets the value of the property alternativeUnitIncrement.
      Property description:
      Specifies the value to add/subtract to the slider's value when an arrow key and Shift or Ctrl are pressed.

      The arrow keys depend on the slider orientation:

      - HORIZONTAL: right, left

      - VERTICAL: up, down

    • getTickUnit

      public double getTickUnit()
      Gets the value of the property tickUnit.
      Property description:
      The value between each major tick mark in data units.
    • tickUnitProperty

      public StyleableDoubleProperty tickUnitProperty()
      The value between each major tick mark in data units.
      See Also:
    • setTickUnit

      public void setTickUnit(double tickUnit)
      Sets the value of the property tickUnit.
      Property description:
      The value between each major tick mark in data units.
    • isShowMajorTicks

      public boolean isShowMajorTicks()
      Gets the value of the property showMajorTicks.
      Property description:
      Specifies if the major ticks should be displayed or not.
    • showMajorTicksProperty

      public StyleableBooleanProperty showMajorTicksProperty()
      Specifies if the major ticks should be displayed or not.
      See Also:
    • setShowMajorTicks

      public void setShowMajorTicks(boolean showMajorTicks)
      Sets the value of the property showMajorTicks.
      Property description:
      Specifies if the major ticks should be displayed or not.
    • isShowMinorTicks

      public boolean isShowMinorTicks()
      Gets the value of the property showMinorTicks.
      Property description:
      Specifies if the minor ticks should be displayed or not.
    • showMinorTicksProperty

      public StyleableBooleanProperty showMinorTicksProperty()
      Specifies if the minor ticks should be displayed or not.
      See Also:
    • setShowMinorTicks

      public void setShowMinorTicks(boolean showMinorTicks)
      Sets the value of the property showMinorTicks.
      Property description:
      Specifies if the minor ticks should be displayed or not.
    • isShowTicksAtEdges

      public boolean isShowTicksAtEdges()
      Gets the value of the property showTicksAtEdges.
      Property description:
      Specifies if the major ticks at the edge of the slider should be displayed or not.

      The ticks at the edge are those ticks which represent the min and max values.

    • showTicksAtEdgesProperty

      public StyleableBooleanProperty showTicksAtEdgesProperty()
      Specifies if the major ticks at the edge of the slider should be displayed or not.

      The ticks at the edge are those ticks which represent the min and max values.

      See Also:
    • setShowTicksAtEdges

      public void setShowTicksAtEdges(boolean showTicksAtEdges)
      Sets the value of the property showTicksAtEdges.
      Property description:
      Specifies if the major ticks at the edge of the slider should be displayed or not.

      The ticks at the edge are those ticks which represent the min and max values.

    • getMinorTicksCount

      public int getMinorTicksCount()
      Gets the value of the property minorTicksCount.
      Property description:
      Specifies how many minor ticks should be added between two major ticks.
    • minorTicksCountProperty

      public StyleableIntegerProperty minorTicksCountProperty()
      Specifies how many minor ticks should be added between two major ticks.
      See Also:
    • setMinorTicksCount

      public void setMinorTicksCount(int minorTicksCount)
      Sets the value of the property minorTicksCount.
      Property description:
      Specifies how many minor ticks should be added between two major ticks.
    • isAnimateOnPress

      public boolean isAnimateOnPress()
      Gets the value of the property animateOnPress.
      Property description:
      When pressing on the slider's track the value is adjusted according to the mouse event coordinates. This property specifies if the progress bar adjustment should be animated or not.
    • animateOnPressProperty

      public StyleableBooleanProperty animateOnPressProperty()
      When pressing on the slider's track the value is adjusted according to the mouse event coordinates. This property specifies if the progress bar adjustment should be animated or not.
      See Also:
    • setAnimateOnPress

      public void setAnimateOnPress(boolean animateOnPress)
      Sets the value of the property animateOnPress.
      Property description:
      When pressing on the slider's track the value is adjusted according to the mouse event coordinates. This property specifies if the progress bar adjustment should be animated or not.
    • isBidirectional

      public boolean isBidirectional()
      Gets the value of the property bidirectional.
      Property description:
      If the slider is set to be bidirectional the progress bar will always start from 0. When the value is negative the progress bar grows in the opposite direction to 0.

      This works only if min is negative and max is positive, otherwise this option in ignored during layout. See the warning in the control documentation.
    • bidirectionalProperty

      public StyleableBooleanProperty bidirectionalProperty()
      If the slider is set to be bidirectional the progress bar will always start from 0. When the value is negative the progress bar grows in the opposite direction to 0.

      This works only if min is negative and max is positive, otherwise this option in ignored during layout. See the warning in the control documentation.
      See Also:
    • setBidirectional

      public void setBidirectional(boolean bidirectional)
      Sets the value of the property bidirectional.
      Property description:
      If the slider is set to be bidirectional the progress bar will always start from 0. When the value is negative the progress bar grows in the opposite direction to 0.

      This works only if min is negative and max is positive, otherwise this option in ignored during layout. See the warning in the control documentation.
    • getOrientation

      public Orientation getOrientation()
      Gets the value of the property orientation.
      Property description:
      Specifies the slider's orientation.
    • orientationProperty

      public StyleableObjectProperty<Orientation> orientationProperty()
      Specifies the slider's orientation.
      See Also:
    • setOrientation

      public void setOrientation(Orientation orientation)
      Sets the value of the property orientation.
      Property description:
      Specifies the slider's orientation.
    • getPopupSide

      public SliderEnums.SliderPopupSide getPopupSide()
      Gets the value of the property popupSide.
      Property description:
      Specifies the popup side.

      DEFAULT is above for horizontal orientation and left for vertical orientation.

      OTHER_SIDE is below for horizontal orientation and right for vertical orientation.

    • popupSideProperty

      Specifies the popup side.

      DEFAULT is above for horizontal orientation and left for vertical orientation.

      OTHER_SIDE is below for horizontal orientation and right for vertical orientation.

      See Also:
    • setPopupSide

      public void setPopupSide(SliderEnums.SliderPopupSide popupSide)
      Sets the value of the property popupSide.
      Property description:
      Specifies the popup side.

      DEFAULT is above for horizontal orientation and left for vertical orientation.

      OTHER_SIDE is below for horizontal orientation and right for vertical orientation.

    • getControlCssMetaDataList

      public static List<CssMetaData<? extends Styleable,?>> getControlCssMetaDataList()
    • createDefaultSkin

      protected Skin<?> createDefaultSkin()
      Overrides:
      createDefaultSkin in class Control
    • getControlCssMetaData

      protected List<CssMetaData<? extends Styleable,?>> getControlCssMetaData()
      Overrides:
      getControlCssMetaData in class Control
    • getUserAgentStylesheet

      public String getUserAgentStylesheet()
      Overrides:
      getUserAgentStylesheet in class Region