Class SliderTicks<N extends Number>
- Type Parameters:
N- The number type of the slider, which is also the type of every spacing and label position.
UIForSlider.withTicks(SliderTicks), or bind a property holding one through
UIForSlider.withTicks(sprouts.Val):
UI.slider(UI.Axis.HORIZONTAL, 0, 100, volume)
.withTicks(
SliderTicks.of(Integer.class)
.withMajorSpacing(25)
.withMinorTicksBetween(4)
.withLabelsAtMajorTicks( v -> v + "%" )
);
This slider draws a major tick mark at 0, 25, 50, 75 and 100, labelled
"0%", "25%", "50%", "75%" and "100%", and four minor tick marks between each
pair of major tick marks, which puts a minor tick mark at every multiple of 5.
Numbers in the slider's own type
Every spacing and every label position is a number of the same type as the value of the slider, which is theN of this class. A slider for
a Double property therefore has its tick marks declared in Doubles:
UI.slider(UI.Axis.HORIZONTAL, 0.0, 1.0, opacity)
.withTicks(
SliderTicks.of(Double.class)
.withMajorSpacing(0.25)
.withTickMarksVisible(false)
.withLabelsAtMajorTicks( v -> Math.round(v * 100) + "%" )
);
A plain JSlider only knows whole numbers, so SwingTree
maps fractional numbers onto a range of whole numbers behind the scenes. It
chooses that range so that every tick mark lands exactly on one of its whole numbers.
Counting starts at the minimum
Tick marks, and the labels at the major tick marks, count from the minimum of the slider. On a slider running from 3 to 97 with a major spacing of 25, the major tick marks sit at 3, 28, 53 and 78. This is howBasicSliderUI,
which the look and feels of Swing build on, draws tick marks, and a label is only useful
where its tick mark is. A label which has to sit
somewhere else can be placed at any number through withLabelAt(Number, String).
It is a value
Every method which sounds like it changes something returns a newSliderTicks,
leaving the one you called it on untouched. Two instances describing the same tick
marks and labels are equals(Object) to each other, which is how a slider bound
to a property holding a SliderTicks knows whether anything actually changed.
A label text function is compared by identity, so a method reference or a lambda
which captures nothing is equal to itself every time it is evaluated.
Since this value holds no Swing component, it is safe to keep in a view model and
to hand from the application thread to the UI thread.
To declare a property of it, use classTyped(Class):
Val<SliderTicks<Integer>> ticks = showTicks.viewAs(
SliderTicks.classTyped(Integer.class),
show -> show ? SliderTicks.of(Integer.class).withMajorSpacing(10) : SliderTicks.of(Integer.class)
);
-
Method Summary
Modifier and TypeMethodDescriptionstatic <N extends Number>
Class<SliderTicks<N>> classTyped(Class<N> numberType) An alternative toSliderTicks.classwhich keeps the number type in the type signature, so that you can declare a property holding aSliderTickswithout casting:booleaninthashCode()booleanTells whether there is a label at every major tick mark.booleanTells whether the tick marks are drawn, or whether only their positions are used.booleanTells whether the knob snaps to the tick marks when the user moves it.labelIconAt(N value) Returns the icon of the label placed at the given number, if it is an icon label.Returns the locale whose conventions the number labels at the major tick marks follow.sprouts.Tuple<N> Returns the numbers at which a label was placed throughwithLabelAt(Number, String)orwithLabelAt(Number, IconDeclaration), in the order they were placed.labelTextAt(N value) Returns the text of the label placed at the given number, if it is a text label.Returns the distance between two neighbouring major tick marks, if there are tick marks.Returns the function computing the text of the labels at the major tick marks, if one was supplied throughwithLabelsAtMajorTicks(Function).intReturns how many minor tick marks sit between two neighbouring major tick marks.Returns the type of the value of the slider, which is also the type of every spacing and label position.static <N extends Number>
SliderTicks<N> Creates a description of no tick marks and no labels for a slider whose value is of the given number type.toString()withLabelAt(N value, String text) Returns a copy with a text label at the given number, replacing any label which was at that number before.withLabelAt(N value, IconDeclaration icon) Returns a copy with an icon label at the given number, replacing any label which was at that number before.withLabelLocale(Locale locale) Returns a copy whose labels at the major tick marks write their numbers the way the given locale does.Returns a copy with a label at every major tick mark, showing the number at that tick mark.withLabelsAtMajorTicks(Function<N, String> text) Returns a copy with a label at every major tick mark, whose text is computed from the number at that tick mark by the given function:withMajorSpacing(N spacing) Returns a copy with major tick marks at the given spacing, counted from the minimum of the slider.withMinorTicksBetween(int count) Returns a copy with the given number of minor tick marks between each pair of neighbouring major tick marks.withoutLabelAt(N value) Returns a copy without the label at the given number, if there is one.Returns a copy without labels at the major tick marks.withSnapToTicks(boolean snap) Returns a copy whose tick marks the knob snaps to when the user moves it, or not, depending on the given flag.withTickMarksVisible(boolean visible) Returns a copy whose tick marks are drawn or not drawn, depending on the given flag.
-
Method Details
-
of
Creates a description of no tick marks and no labels for a slider whose value is of the given number type. This is where everySliderTicksstarts out:
The supported number types areSliderTicks.of(Integer.class).withMajorSpacing(10)Integer,Long,Short,Byte,FloatandDouble, which are also the number types a slider can be bound to.- Type Parameters:
N- The number type of the slider.- Parameters:
numberType- The type of the value of the slider, and therefore of every spacing and label position.- Returns:
- A
SliderTickswith no tick marks and no labels. - Throws:
NullPointerException- IfnumberTypeisnull.IllegalArgumentException- IfnumberTypeis not one of the supported number types.
-
classTyped
An alternative toSliderTicks.classwhich keeps the number type in the type signature, so that you can declare a property holding aSliderTickswithout casting:Var<SliderTicks<Double>> ticks = Var.of(SliderTicks.classTyped(Double.class), SliderTicks.of(Double.class));- Type Parameters:
N- The number type of the slider.- Parameters:
numberType- The number typeNin the returnedClass<SliderTicks<N>>.- Returns:
- The
SliderTicks.class, typed asClass<SliderTicks<N>>. - Throws:
NullPointerException- IfnumberTypeisnull.
-
numberType
Returns the type of the value of the slider, which is also the type of every spacing and label position.- Returns:
- The number type of the slider.
-
withMajorSpacing
Returns a copy with major tick marks at the given spacing, counted from the minimum of the slider. A spacing of 10 on a slider running from 0 to 100 puts a major tick mark at 0, 10, 20 and so on up to 100. The major spacing is also how far the knob moves when the user clicks into the track beside it, or presses page up or page down, unless there are minor tick marks, whose spacing is used instead.A spacing of zero removes the tick marks, and with them the labels at the major tick marks.
- Parameters:
spacing- The distance between two neighbouring major tick marks, in the numbers of the slider.- Returns:
- A new
SliderTickswith the given major spacing. - Throws:
NullPointerException- Ifspacingisnull.IllegalArgumentException- Ifspacingis negative, not a number or infinite.
-
majorSpacing
Returns the distance between two neighbouring major tick marks, if there are tick marks.- Returns:
- The major spacing, or an empty
Optionalif there are no tick marks.
-
withMinorTicksBetween
Returns a copy with the given number of minor tick marks between each pair of neighbouring major tick marks. Four minor tick marks between major tick marks which are 25 apart divide that distance into five equal parts, so a minor tick mark sits at every multiple of 5. Zero means that there are no minor tick marks.Counting the tick marks between the major ones, instead of declaring a second spacing, makes it impossible to declare minor tick marks which miss the major ones. On a slider for whole numbers the major spacing must still be divisible into that many equal whole number parts: a major spacing of 25 with 3 minor tick marks between them would need a minor tick mark every 6.25, so SwingTree logs a warning and draws no minor tick marks.
- Parameters:
count- The number of minor tick marks between two neighbouring major tick marks.- Returns:
- A new
SliderTickswith the given number of minor tick marks. - Throws:
IllegalArgumentException- Ifcountis negative.
-
minorTicksBetween
public int minorTicksBetween()Returns how many minor tick marks sit between two neighbouring major tick marks.- Returns:
- The number of minor tick marks between two neighbouring major tick marks.
-
withTickMarksVisible
Returns a copy whose tick marks are drawn or not drawn, depending on the given flag. Tick marks are drawn by default.Tick marks which are not drawn still exist: the labels at the major tick marks are still shown, and the knob still snaps to them if
withSnapToTicks(boolean)says so. This is how you build a slider with labels at 0, 25, 50, 75 and 100 but without any marks on its track.- Parameters:
visible-trueto draw the tick marks,falseto only use their positions.- Returns:
- A new
SliderTickswith the given visibility of its tick marks.
-
hasVisibleTickMarks
public boolean hasVisibleTickMarks()Tells whether the tick marks are drawn, or whether only their positions are used.- Returns:
trueif the tick marks are drawn,falseif only their positions are used.
-
withSnapToTicks
Returns a copy whose tick marks the knob snaps to when the user moves it, or not, depending on the given flag. The knob snaps to the minor tick marks if there are any, and to the major tick marks otherwise. Without a major spacing there is nothing to snap to, and the flag has no effect.While the user drags the knob, the value of the slider is the number at the nearest tick mark, and when the user lets go of the knob, it moves onto that tick mark. An arrow key moves the knob to the next tick mark in the direction of the key.
Snapping only ever applies to what the user does. When your application sets the value of the slider to a number between two tick marks, the knob sits between those tick marks, and the value is not changed behind your back.
- Parameters:
snap-trueto let the knob snap to the tick marks,falseto let it move freely.- Returns:
- A new
SliderTickswith the given snapping behaviour.
-
isSnappingToTicks
public boolean isSnappingToTicks()Tells whether the knob snaps to the tick marks when the user moves it.- Returns:
trueif the knob snaps to the tick marks when the user moves it.
-
withLabelsAtMajorTicks
Returns a copy with a label at every major tick mark, showing the number at that tick mark. All labels show their numbers with the same count of decimal places, namely the fewest which write every one of them exactly: labels at 0, 0.25, 0.5, 0.75 and 1 read "0.00", "0.25", "0.50", "0.75" and "1.00".The numbers are written independently of the locale of the machine, with a dot as decimal separator and without grouping, so "1234.5" reads the same everywhere. To write them the way a particular locale does, use
withLabelLocale(Locale).The labels only exist while there is a major spacing, see
withMajorSpacing(Number).- Returns:
- A new
SliderTickswith a number label at every major tick mark.
-
withLabelsAtMajorTicks
Returns a copy with a label at every major tick mark, whose text is computed from the number at that tick mark by the given function:
The function receives the exact number at the tick mark, computed as the minimum of the slider plus a whole multiple of the major spacing, so a slider from 0.0 with a major spacing of 0.1 hands 0.3 to the function, and never 0.30000000000000004.SliderTicks.of(Integer.class).withMajorSpacing(25).withLabelsAtMajorTicks( v -> v + "%" )The labels only exist while there is a major spacing, see
withMajorSpacing(Number).- Parameters:
text- The function computing the text of a label from the number at its tick mark.- Returns:
- A new
SliderTickswith a label at every major tick mark. - Throws:
NullPointerException- Iftextisnull.
-
withoutLabelsAtMajorTicks
Returns a copy without labels at the major tick marks. The labels placed at particular numbers throughwithLabelAt(Number, String)are kept.- Returns:
- A new
SliderTickswithout labels at the major tick marks.
-
hasLabelsAtMajorTicks
public boolean hasLabelsAtMajorTicks()Tells whether there is a label at every major tick mark.- Returns:
trueif there is a label at every major tick mark.
-
majorTickLabelText
Returns the function computing the text of the labels at the major tick marks, if one was supplied throughwithLabelsAtMajorTicks(Function).- Returns:
- The label text function, or an empty
Optionalif the labels at the major tick marks show their numbers, or if there are no labels at the major tick marks.
-
withLabelLocale
Returns a copy whose labels at the major tick marks write their numbers the way the given locale does. WithLocale.GERMANYa label at 1234.5 reads "1.234,5", whereas withLocale.USit reads "1,234.5".The default is
Locale.ROOT, which writes numbers independently of any locale: with a dot as decimal separator and without grouping, so the same label reads "1234.5". PassLocale.getDefault()to follow the locale of the machine.The locale only affects labels showing numbers through
withLabelsAtMajorTicks(). A text function passed towithLabelsAtMajorTicks(Function)does its own formatting.- Parameters:
locale- The locale whose conventions the number labels follow.- Returns:
- A new
SliderTickswith the given label locale. - Throws:
NullPointerException- Iflocaleisnull.
-
labelLocale
Returns the locale whose conventions the number labels at the major tick marks follow.- Returns:
- The label locale, which is
Locale.ROOTunlesswithLabelLocale(Locale)says otherwise.
-
withLabelAt
Returns a copy with a text label at the given number, replacing any label which was at that number before. The number does not need to be at a tick mark:
A label at a number outside the range of the slider is not shown. When a label at a particular number and a label at a major tick mark fall onto the same position, the label at the particular number is shown.SliderTicks.of(Integer.class) .withLabelAt(0, "Cold") .withLabelAt(50, "Warm") .withLabelAt(100, "Hot")- Parameters:
value- The number at which the label is shown.text- The text of the label.- Returns:
- A new
SliderTickswith the given label. - Throws:
NullPointerException- Ifvalueortextisnull.
-
withLabelAt
Returns a copy with an icon label at the given number, replacing any label which was at that number before. This is how a volume slider shows a muted speaker at its start and a loud speaker at its end:
A label at a number outside the range of the slider is not shown. When a label at a particular number and a label at a major tick mark fall onto the same position, the label at the particular number is shown.SliderTicks.of(Integer.class) .withLabelAt(0, Icons.MUTED) .withLabelAt(100, Icons.LOUD)- Parameters:
value- The number at which the label is shown.icon- The icon of the label.- Returns:
- A new
SliderTickswith the given label. - Throws:
NullPointerException- Ifvalueoriconisnull.
-
withoutLabelAt
Returns a copy without the label at the given number, if there is one. The labels at the major tick marks are not affected.- Parameters:
value- The number of the label to remove.- Returns:
- A new
SliderTickswithout a label at the given number. - Throws:
NullPointerException- Ifvalueisnull.
-
labelPositions
Returns the numbers at which a label was placed throughwithLabelAt(Number, String)orwithLabelAt(Number, IconDeclaration), in the order they were placed.- Returns:
- The positions of the labels placed at particular numbers.
-
labelTextAt
Returns the text of the label placed at the given number, if it is a text label.- Parameters:
value- The number of the label.- Returns:
- The text of the label at the given number, or an empty
Optionalif there is no label at that number, or if it is an icon label.
-
labelIconAt
Returns the icon of the label placed at the given number, if it is an icon label.- Parameters:
value- The number of the label.- Returns:
- The icon of the label at the given number, or an empty
Optionalif there is no label at that number, or if it is a text label.
-
equals
-
hashCode
public int hashCode() -
toString
-