Slider

@Composable
fun Slider(value: Int, onValueChange: (Int) -> Unit, modifier: SwingModifier = SwingModifier, onValueSettled: (Int) -> Unit = {}, min: Int = 0, max: Int = 100, orientation: Int = SwingConstants.HORIZONTAL, inverted: Boolean = false, majorTickSpacing: Int = 0, minorTickSpacing: Int = 0, paintTicks: Boolean = false, paintLabels: Boolean = false, labels: Map<Int, @Nls String>? = null, snapToTicks: Boolean = false)

A JSlider: the user picks a whole number between min and max by dragging a knob along a track. A value the user leaves the knob on and the caller does not answer with a matching value is settled back onto the declared one once the drag is released, so the knob ends where the composition says.

A drag is published a value at a time: the slider passes through every value between the one it was grabbed at and the one it is let go on, and each of them reaches onValueChange as the user reaches it, while onValueSettled hears the value the drag was released on and none of the ones it passed through. A caller that follows the drag adopts onValueChange; a caller that acts on the released value alone - one that starts work too expensive to run per step - takes onValueSettled and leaves value where it is until then, and the slider follows the mouse in the meantime.

Parameters

value

the current value

onValueChange

callback invoked with every value the user moves the slider to, the ones a drag passes through included, and with the value the slider is left on where it cannot hold value - one outside the range, or one off the grid snapToTicks resolves to; applying a value the slider can hold is not itself reported

modifier

the SwingModifier applied to the underlying component

onValueSettled

callback invoked with the value the slider settles on: the one a drag is released on, one the user reaches outside a drag, and the value the slider is left on where it cannot hold value

min

the smallest value the slider can hold; 0 by default

max

the largest value the slider can hold; 100 by default

orientation

the orientation of the slider (an Orientation SwingConstants value); HORIZONTAL by default, which runs the track across the width the slider is given

inverted

whether the value axis runs backwards, with the maximum at the left or bottom end; false by default, which puts the minimum at that end instead

majorTickSpacing

the value distance between major tick marks, 0 for none - the default

minorTickSpacing

the value distance between minor tick marks, 0 for none - the default

paintTicks

whether the tick marks are painted; false by default, which draws a bare track

paintLabels

whether the value labels are painted; false by default, which draws no text along the track

labels

the text to draw at each value, keyed by the value the label sits at. null leaves the labels to Swing, which draws one at every major tick mark when paintLabels is true and majorTickSpacing is positive

snapToTicks

whether a value the user picks resolves to the closest tick mark; false by default, which lets the knob rest on any value in the range. The grid is minorTickSpacing where it is positive and majorTickSpacing otherwise, so with neither declared there is no grid to snap to

See also


@Composable
fun Slider(value: Int, changeListener: ChangeListener, modifier: SwingModifier = SwingModifier, min: Int = 0, max: Int = 100, orientation: Int = SwingConstants.HORIZONTAL, inverted: Boolean = false, majorTickSpacing: Int = 0, minorTickSpacing: Int = 0, paintTicks: Boolean = false, paintLabels: Boolean = false, labels: Map<Int, @Nls String>? = null, snapToTicks: Boolean = false)

A Slider driven by a raw ChangeListener instead of the onValueChange and onValueSettled lambdas. The changeListener is attached as-is and removed on the same instance; pass a stable instance (e.g. remember {}) to avoid a detach/re-attach on every recomposition. Being attached as-is, it is notified of every change to the slider's value - the values a drag passes through as well as the one it settles on, and the change that applies value included - and reads getValueIsAdjusting off the slider to tell them apart.

Parameters

value

the current value

changeListener

the listener notified when the value changes

modifier

the SwingModifier applied to the underlying component

min

the smallest value the slider can hold; 0 by default

max

the largest value the slider can hold; 100 by default

orientation

the orientation of the slider (an Orientation SwingConstants value); HORIZONTAL by default, which runs the track across the width the slider is given

inverted

whether the value axis runs backwards, with the maximum at the left or bottom end; false by default, which puts the minimum at that end instead

majorTickSpacing

the value distance between major tick marks, 0 for none - the default

minorTickSpacing

the value distance between minor tick marks, 0 for none - the default

paintTicks

whether the tick marks are painted; false by default, which draws a bare track

paintLabels

whether the value labels are painted; false by default, which draws no text along the track

labels

the text to draw at each value, keyed by the value the label sits at. null leaves the labels to Swing, which draws one at every major tick mark when paintLabels is true and majorTickSpacing is positive

snapToTicks

whether a value the user picks resolves to the closest tick mark; false by default, which lets the knob rest on any value in the range. The grid is minorTickSpacing where it is positive and majorTickSpacing otherwise, so with neither declared there is no grid to snap to

See also


@Composable
fun Slider(model: BoundedRangeModel, modifier: SwingModifier = SwingModifier, onValueChange: (Int) -> Unit = {}, onValueSettled: (Int) -> Unit = {}, orientation: Int = SwingConstants.HORIZONTAL, inverted: Boolean = false, majorTickSpacing: Int = 0, minorTickSpacing: Int = 0, paintTicks: Boolean = false, paintLabels: Boolean = false, labels: Map<Int, @Nls String>? = null, snapToTicks: Boolean = false)

A Slider over a caller-owned BoundedRangeModel. The model owns the value and the range, so nothing is declared over it: the slider renders whatever the model holds, the library never writes to it, and a model the caller mutates repaints the slider without a recomposition. Supplying a new model instance installs it on recomposition.

This is what lets one range drive several widgets - a slider and the ProgressBar reading it out, or two views of the same position - since each of them renders the model as-is:

val range = remember { DefaultBoundedRangeModel(30, 0, 0, 100) }
Slider(model = range)
ProgressBar(model = range)

A drag reaches onValueChange a value at a time and onValueSettled once, on the value it is released on; see the declared-value Slider for what each channel carries.

Parameters

model

the range the slider renders and the user moves; owned by the caller and never written to by the library

modifier

the SwingModifier applied to the underlying component

onValueChange

callback invoked with every value the slider publishes, the ones a drag passes through included

onValueSettled

callback invoked with the value the slider settles on: the one a drag is released on, and one the value reaches outside a drag

orientation

the orientation of the slider (an Orientation SwingConstants value); HORIZONTAL by default, which runs the track across the width the slider is given

inverted

whether the value axis runs backwards, with the maximum at the left or bottom end; false by default, which puts the minimum at that end instead

majorTickSpacing

the value distance between major tick marks, 0 for none - the default

minorTickSpacing

the value distance between minor tick marks, 0 for none - the default

paintTicks

whether the tick marks are painted; false by default, which draws a bare track

paintLabels

whether the value labels are painted; false by default, which draws no text along the track

labels

the text to draw at each value, keyed by the value the label sits at. null leaves the labels to Swing, which draws one at every major tick mark when paintLabels is true and majorTickSpacing is positive

snapToTicks

whether a value the user picks resolves to the closest tick mark; false by default, which lets the knob rest on any value in the range. The grid is minorTickSpacing where it is positive and majorTickSpacing otherwise, so with neither declared there is no grid to snap to

See also


@Composable
fun Slider(model: BoundedRangeModel, changeListener: ChangeListener, modifier: SwingModifier = SwingModifier, orientation: Int = SwingConstants.HORIZONTAL, inverted: Boolean = false, majorTickSpacing: Int = 0, minorTickSpacing: Int = 0, paintTicks: Boolean = false, paintLabels: Boolean = false, labels: Map<Int, @Nls String>? = null, snapToTicks: Boolean = false)

A model-driven Slider driven by a raw ChangeListener instead of the onValueChange and onValueSettled lambdas. The model is rendered as-is and never written to by the library. The changeListener is attached as-is and removed on the same instance; pass a stable instance (e.g. remember {}) to avoid churn, and read getValueIsAdjusting off the slider to tell the values a drag passes through from the one it settles on.

Parameters

model

the range the slider renders and the user moves; owned by the caller and never written to by the library

changeListener

the listener notified when the value changes

modifier

the SwingModifier applied to the underlying component

orientation

the orientation of the slider (an Orientation SwingConstants value); HORIZONTAL by default, which runs the track across the width the slider is given

inverted

whether the value axis runs backwards, with the maximum at the left or bottom end; false by default, which puts the minimum at that end instead

majorTickSpacing

the value distance between major tick marks, 0 for none - the default

minorTickSpacing

the value distance between minor tick marks, 0 for none - the default

paintTicks

whether the tick marks are painted; false by default, which draws a bare track

paintLabels

whether the value labels are painted; false by default, which draws no text along the track

labels

the text to draw at each value, keyed by the value the label sits at. null leaves the labels to Swing, which draws one at every major tick mark when paintLabels is true and majorTickSpacing is positive

snapToTicks

whether a value the user picks resolves to the closest tick mark; false by default, which lets the knob rest on any value in the range. The grid is minorTickSpacing where it is positive and majorTickSpacing otherwise, so with neither declared there is no grid to snap to

See also