- Type Parameters:
S- The type ofJSliderthat thisUIForSlideris configuring.N- The type of the numbers the slider works with.
JSlider instances.
Please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.
A slider in SwingTree works with numbers of the type N, which is
Integer unless you created the slider through one of the factory methods
taking properties of another number type, like
UIFactoryMethods.slider(UI.Axis, Number, Number, Var) with a Var<Double>.
The minimum, the maximum, the value and the tick marks of the slider are all
expressed in that type, even though a plain JSlider only knows whole numbers.
On a slider for whole numbers, a minimum or maximum set directly through
JSlider.setMinimum(int) or JSlider.setMaximum(int) becomes the range
of the slider, and its tick marks and labels are laid out again for that range.
On a slider for Float or Double numbers, the whole numbers of the
JSlider are an internal detail, so such a call is undone right away.
-
Method Summary
Modifier and TypeMethodDescriptionprotected UIForSlider<S, N> _newBuilderWithState(swingtree.BuilderState<S> newState) An internal wither method which creates a new builder instance with the providedBuilderStatestored inside it.protected swingtree.BuilderState<S> _state()Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor.final UIForSlider<S, N> onChange(sprouts.Action<ComponentDelegate<JSlider, ChangeEvent>> action) Adds anActionwhich is called when the user changes the state of the slider, for example by moving its knob, or by pressing or releasing the mouse button on it.final UIForSlider<S, N> withMajorTickSpacing(N spacing) Sets how far apart the major tick marks of the slider are, in the numbers of the slider.final UIForSlider<S, N> withMajorTickSpacing(sprouts.Val<N> spacing) Binds the suppliedValproperty to the spacing of the major tick marks of the slider, so that the major tick marks move whenever the item of the property changes.final UIForSlider<S, N> Sets the maximum value of the slider, which is the number at the end of its track.final UIForSlider<S, N> Binds the suppliedValproperty to the maximum value of the slider, so that when the value of the property changes, the maximum of the slider, and with it the position of the knob and of the tick marks, is updated accordingly.final UIForSlider<S, N> Sets the minimum value of the slider, which is the number at the start of its track.final UIForSlider<S, N> Binds the suppliedValproperty to the minimum value of the slider, so that when the value of the property changes, the minimum of the slider, and with it the position of the knob and of the tick marks, is updated accordingly.final UIForSlider<S, N> withMinorTickSpacing(N spacing) Sets how far apart the minor tick marks of the slider are, in the numbers of the slider.final UIForSlider<S, N> withMinorTickSpacing(sprouts.Val<N> spacing) Binds the suppliedValproperty to the spacing of the minor tick marks of the slider, so that the minor tick marks move whenever the item of the property changes.final UIForSlider<S, N> withOrientation(sprouts.Val<UI.Axis> axis) Dynamically sets the orientation of the slider.final UIForSlider<S, N> withOrientation(UI.Axis axis) Sets the orientation of the slider.final UIForSlider<S, N> withTicks(sprouts.Val<SliderTicks<N>> ticks) Binds the tick marks and labels along the slider to the supplied property holding aSliderTicksvalue, so that the slider shows the tick marks and labels of the property's current item, and follows every change of it.final UIForSlider<S, N> withTicks(SliderTicks<N> ticks) Configures the tick marks and labels along the slider through the suppliedSliderTicksvalue, which describes where the major and minor tick marks are, whether they are drawn, whether the knob snaps to them and which labels are shown:final UIForSlider<S, N> Sets the current value of the slider, which places the knob at that number.final UIForSlider<S, N> Binds the suppliedValproperty to the value of the slider, which causes the knob of the slider to move when the value of the property changes.final UIForSlider<S, N> Use this to bind the suppliedVarproperty to the value of the slider.Methods inherited from class swingtree.UIForAnySwing
_addComponentTo, _addViewableProps, _addViewableProps, _addViewableProps, _isUndefinedColor, _isUndefinedFont, _isUndefinedFont, _setBackground, _setEnabled, _setMinHeight, _setMinWidth, _setPrefWidth, _tryCalculatingDiffBetween, add, add, add, add, add, add, add, add, add, add, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, doUpdates, group, group, id, id, isEnabledIf, isEnabledIf, isEnabledIf, isEnabledIfNot, isEnabledIfNot, isEnabledIfNot, isFocusableIf, isFocusableIf, isFocusableIf, isFocusableIfNot, isFocusableIfNot, isFocusableIfNot, isValidIf, isVisibleIf, isVisibleIf, isVisibleIf, isVisibleIfNot, isVisibleIfNot, isVisibleIfNot, makeFocused, on, on, onCharTyped, onFocusGain, onFocusLoss, onHidden, onKeyPress, onKeyRelease, onKeyTyped, onMouseClick, onMouseDrag, onMouseEnter, onMouseEnter, onMouseEnterGreedy, onMouseExit, onMouseExit, onMouseExitGreedy, onMouseMove, onMousePress, onMouseRelease, onMouseWheelDown, onMouseWheelMove, onMouseWheelUp, onMoved, onPressed, onRelease, onResize, onShown, onTyped, onTyped, onView, withBackground, withBackground, withBackgroundColor, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBorder, withBorderTitled, withBorderTitled, withBounds, withBounds, withBoxLayout, withCompoundBorder, withCompoundBorderTitled, withCursor, withCursor, withCursorIf, withCursorIf, withDragAway, withDropSite, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withFlowLayout, withFlowLayout, withFlowLayout, withFont, withFont, withFont, withFontSize, withFontSize, withForeground, withForeground, withForegroundColor, withForegroundIf, withForegroundIf, withForegroundIf, withForegroundIf, withGridBagLayout, withGridLayout, withGridLayout, withGridLayout, withGridLayout, withGridLayout, withGridLayout, withGridLayout, withHeight, withHeight, withHeightExactly, withHeightExactly, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLineBorder, withLineBorder, withLineBorder, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withLocation, withLocation, withMatteBorder, withMatteBorder, withMatteBorder, withMatteBorderTitled, withMatteBorderTitled, withMatteBorderTitled, withMaxHeight, withMaxHeight, withMaxSize, withMaxSize, withMaxSize, withMaxSize, withMaxWidth, withMaxWidth, withMinHeight, withMinHeight, withMinSize, withMinSize, withMinSize, withMinSize, withMinWidth, withMinWidth, withPrefHeight, withPrefHeight, withPrefSize, withPrefSize, withPrefSize, withPrefSize, withPrefWidth, withPrefWidth, withProperty, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withSize, withSize, withSize, withSizeExactly, withSizeExactly, withSizeExactly, withSizeExactly, withStyle, withStyle, withStyle, withTooltip, withTooltip, withTransitionalStyle, withTransitoryStyle, withWidth, withWidth, withWidthExactly, withWidthExactlyMethods inherited from class swingtree.UIForAnything
_addBuildersTo, _addBuilderTo, _addComponentsTo, _disposeState, _onShow, _onShow, _onShowDelegated, _runInApp, _runInApp, _runInUI, _this, _with, _withOnShow, _withOnShow, add, add, add, add, apply, applyIf, applyIfPresent, equals, get, getType, hashCode, peek, toString
-
Method Details
-
_state
Description copied from class:UIForAnythingReturns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor.- Specified by:
_statein classUIForAnything<UIForSlider<S extends JSlider,N extends Number>, S extends JSlider, JComponent> - Returns:
- The state of the builder.
-
_newBuilderWithState
Description copied from class:UIForAnythingAn internal wither method which creates a new builder instance with the providedBuilderStatestored inside it.- Specified by:
_newBuilderWithStatein classUIForAnything<UIForSlider<S extends JSlider,N extends Number>, S extends JSlider, JComponent> - Parameters:
newState- The new state which should be stored inside the new builder instance.- Returns:
- A new builder instance with the provided state stored inside it.
-
withOrientation
Sets the orientation of the slider.- Parameters:
axis- The orientation of the slider.- Returns:
- This builder node.
-
withOrientation
Dynamically sets the orientation of the slider.- Parameters:
axis- The orientation of the slider.- Returns:
- This builder node.
-
onChange
public final UIForSlider<S,N> onChange(sprouts.Action<ComponentDelegate<JSlider, ChangeEvent>> action) Adds anActionwhich is called when the user changes the state of the slider, for example by moving its knob, or by pressing or releasing the mouse button on it. Changes which your application makes through the properties or values given to this builder do not call the action.Every change calls each action once, in the order in which the actions were added. When the knob snaps to a tick mark (see
SliderTicks.withSnapToTicks(boolean)), the actions are called after the knob has snapped, so they read the number at that tick mark.- Parameters:
action- TheActionthat will be called through the underlying change event.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifactionisnull.
-
withMin
Sets the minimum value of the slider, which is the number at the start of its track.- Parameters:
min- The minimum value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifminisnull.
-
withMin
Binds the suppliedValproperty to the minimum value of the slider, so that when the value of the property changes, the minimum of the slider, and with it the position of the knob and of the tick marks, is updated accordingly. When the user moves the knob, the value written back by the slider is never smaller than the item of this property.- Parameters:
min- The property holding the minimum value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifminisnullor allowsnullitems.
-
withMax
Sets the maximum value of the slider, which is the number at the end of its track.- Parameters:
max- The maximum value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifmaxisnull.
-
withMax
Binds the suppliedValproperty to the maximum value of the slider, so that when the value of the property changes, the maximum of the slider, and with it the position of the knob and of the tick marks, is updated accordingly. When the user moves the knob, the value written back by the slider is never larger than the item of this property.- Parameters:
max- The property holding the maximum value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifmaxisnullor allowsnullitems.
-
withValue
Sets the current value of the slider, which places the knob at that number. A number outside the range of the slider places the knob at the nearest end of the track.- Parameters:
value- The current value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifvalueisnull.
-
withValue
Binds the suppliedValproperty to the value of the slider, which causes the knob of the slider to move when the value of the property changes. But note that the supplied property is a read only, so when the user moves the knob, the property will not be updated. UsewithValue(Var)if you want to bind a property bidirectionally.While the user holds the knob with the mouse, changes of the property do not move it.
- Parameters:
value- A property used to dynamically update the value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifvalueisnullor allowsnullitems.
-
withValue
Use this to bind the suppliedVarproperty to the value of the slider. When the user moves the knob, theVaris updated, and when the item of theVaris changed as part of the application logic, the knob moves accordingly.The number written into the property is of the property's own type, and it is the number at the position of the knob, or, if the knob snaps to tick marks (see
SliderTicks.withSnapToTicks(boolean)), the number at the tick mark it snaps to. A click on the knob which does not move it writes nothing new, so the property keeps exactly the number your application set.While the user holds the knob with the mouse, changes of the property do not move it, and when the user lets go, the number under the knob is written into the property.
- Parameters:
value- A property holding the value of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifvalueisnullor allowsnullitems.
-
withMajorTickSpacing
Sets how far apart the major tick marks of the slider are, in the numbers of the slider. The major tick marks count from the minimum of the slider, so a spacing of 25 on a slider running from 0 to 100 puts a major tick mark at 0, 25, 50, 75 and 100. A spacing of zero or less removes the major tick marks.Like
JSlider.setMajorTickSpacing(int), this method only sets where the major tick marks are. The slider draws them onceJSlider.setPaintTicks(boolean)was called withtrue, for example throughpeek( s -> s.setPaintTicks(true) ). UnlikeJSlider.setMajorTickSpacing(int), the spacing is a number of the slider's own type, so a slider for aDoubleproperty can have a major tick mark every0.25.To describe tick marks, labels and snapping together, use
withTicks(SliderTicks)instead. A slider which has been given aSliderTicksvalue takes its tick marks from that value and ignores the spacing given to this method, no matter in which order the two methods are called.- Parameters:
spacing- The distance between two neighbouring major tick marks, in the numbers of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifspacingisnull.
-
withMajorTickSpacing
Binds the suppliedValproperty to the spacing of the major tick marks of the slider, so that the major tick marks move whenever the item of the property changes. The spacing is a number of the slider's own type, and the major tick marks count from the minimum of the slider, so a spacing of 25 on a slider running from 0 to 100 puts a major tick mark at 0, 25, 50, 75 and 100. A spacing of zero or less removes the major tick marks.Like
JSlider.setMajorTickSpacing(int), the property only decides where the major tick marks are. The slider draws them onceJSlider.setPaintTicks(boolean)was called withtrue. A slider which has been given aSliderTicksvalue throughwithTicks(SliderTicks)orwithTicks(Val)takes its tick marks from that value and ignores this property.- Parameters:
spacing- A property holding the distance between two neighbouring major tick marks, in the numbers of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifspacingisnullor allowsnullitems.
-
withMinorTickSpacing
Sets how far apart the minor tick marks of the slider are, in the numbers of the slider. The minor tick marks count from the minimum of the slider, so a spacing of 5 on a slider running from 0 to 100 puts a minor tick mark at 0, 5, 10 and so on up to 100. A spacing of zero or less removes the minor tick marks.Like
JSlider.setMinorTickSpacing(int), this method only sets where the minor tick marks are. The slider draws them onceJSlider.setPaintTicks(boolean)was called withtrue, for example throughpeek( s -> s.setPaintTicks(true) ). UnlikeJSlider.setMinorTickSpacing(int), the spacing is a number of the slider's own type, so a slider for aDoubleproperty can have a minor tick mark every0.05.The minor tick marks do not depend on the major tick marks. With a major spacing of 25 and a minor spacing of 10, the minor tick marks sit at 0, 10, 20, 30 and 40, while the major tick marks sit at 0, 25 and 50, so the minor tick marks do not divide the gaps between the major ones evenly.
SliderTicks.withMinorTicksBetween(int)counts the minor tick marks between two major tick marks instead, which always divides the gaps evenly. A slider which has been given aSliderTicksvalue throughwithTicks(SliderTicks)takes its tick marks from that value and ignores the spacing given to this method, no matter in which order the two methods are called.- Parameters:
spacing- The distance between two neighbouring minor tick marks, in the numbers of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifspacingisnull.
-
withMinorTickSpacing
Binds the suppliedValproperty to the spacing of the minor tick marks of the slider, so that the minor tick marks move whenever the item of the property changes. The spacing is a number of the slider's own type, and the minor tick marks count from the minimum of the slider, so a spacing of 5 on a slider running from 0 to 100 puts a minor tick mark at 0, 5, 10 and so on up to 100. A spacing of zero or less removes the minor tick marks.Like
JSlider.setMinorTickSpacing(int), the property only decides where the minor tick marks are. The slider draws them onceJSlider.setPaintTicks(boolean)was called withtrue. A slider which has been given aSliderTicksvalue throughwithTicks(SliderTicks)orwithTicks(Val)takes its tick marks from that value and ignores this property.- Parameters:
spacing- A property holding the distance between two neighbouring minor tick marks, in the numbers of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifspacingisnullor allowsnullitems.
-
withTicks
Configures the tick marks and labels along the slider through the suppliedSliderTicksvalue, which describes where the major and minor tick marks are, whether they are drawn, whether the knob snaps to them and which labels are shown:
The spacings and label positions are numbers of the slider's own type, so a slider for aUI.slider(UI.Axis.HORIZONTAL, 0, 100, volume) .withTicks( SliderTicks.of(Integer.class) .withMajorSpacing(25) .withMinorTicksBetween(4) .withLabelsAtMajorTicks( v -> v + "%" ) )Doubleproperty is configured with aSliderTicks<Double>. Whenever the minimum or maximum of the slider changes, the tick marks and labels are laid out again for the new range.- Parameters:
ticks- The tick marks and labels of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifticksisnull.
-
withTicks
Binds the tick marks and labels along the slider to the supplied property holding aSliderTicksvalue, so that the slider shows the tick marks and labels of the property's current item, and follows every change of it.The property may live in your view model, or be derived from view model state right in the view, like this slider which shows its labels only while a flag says so:
Under theSliderTicks<Integer> plain = SliderTicks.of(Integer.class).withMajorSpacing(25); SliderTicks<Integer> labelled = plain.withLabelsAtMajorTicks(); UI.slider(UI.Axis.HORIZONTAL, 0, 100, volume) .withTicks( showLabels.viewAs(SliderTicks.classTyped(Integer.class), show -> show ? labelled : plain) )EventProcessor.DECOUPLEDevent processor the newSliderTicksreaches the slider as part of the property's change event, in the same order as every other change of the slider's properties.- Parameters:
ticks- A property holding the tick marks and labels of the slider.- Returns:
- This very instance, which enables builder-style method chaining.
- Throws:
IllegalArgumentException- ifticksisnullor allowsnullitems.
-