Overview
JSlider is a customizable slider component that extends JavaFX’s Slider with an enhanced visual design featuring a gradient track that fills based on the current value.
Constructors
JSlider()
Creates a slider with default range (0 to 100) and initial value at 0.JSlider(double min, double max, double value)
Creates a slider with specified minimum, maximum, and initial value.The minimum value of the slider
The maximum value of the slider
The initial value of the slider
Features
Dynamic Track Fill
The slider automatically updates its track visualization to show a gradient fill from the minimum value to the current value. The filled portion uses the primary theme color, while the unfilled portion uses a neutral gray.- The track fill updates automatically when the value changes
- The fill percentage is calculated based on the current value relative to the min/max range
- Uses CSS linear gradient for smooth visual appearance
Inherited Properties
As an extension of JavaFX’sSlider, JSlider inherits all standard slider properties:
value
The current value of the slider.min
The minimum value of the slider.max
The maximum value of the slider.blockIncrement
The amount to increment/decrement when using keyboard navigation.majorTickUnit
The spacing between major tick marks.showTickMarks
Whether to display tick marks.showTickLabels
Whether to display tick labels.snapToTicks
Whether the slider snaps to tick marks.orientation
The orientation of the slider (HORIZONTAL or VERTICAL).Inherited Methods
setValue(double value)
Sets the current value of the slider.The value to set (clamped between min and max)
getValue()
Returns the current value.The current value of the slider
valueProperty()
Returns the value property for binding.The observable property for the slider value
setMin(double value)
Sets the minimum value.The minimum value
setMax(double value)
Sets the maximum value.The maximum value
Example Usage
CSS Style Classes
The component applies the following style class:j-slider- Base style class for custom styling
Implementation Details
Track Fill Update
The slider uses a listener on thevalueProperty() to dynamically update the track fill:
- Calculates the percentage of the current value relative to the range
- Applies a CSS linear gradient to the
.tracknode - The gradient transitions from the primary color to a neutral gray at the calculated percentage
- The fill percentage is clamped between 0% and 100%
Initialization Timing
The component waits for the slider’s skin to be applied before performing the initial track fill update. This ensures that the.track node is available in the scene graph.