Skip to content

Scroller

Since 2.2.1

Scroller is a scroll bar control that holds a value in a [min, max] range. Two concrete variants are registered:

  • scroller-vertical — a thin vertical bar (5 px wide by default).
  • scroller-horizontal — a thin horizontal bar (5 px tall by default).

Both expose a head button (scroll up/left), a tail button (scroll down/right), a drag-handle scrollBar, and a scrollContainer track. Clicking the track jumps the value to the clicked position. Scrolling the mouse wheel also changes the value.

Scroller is primarily used as an internal component inside ScrollerView and TextArea, but it can also be used standalone.

INFO

Everything documented on UIElement (layout, styles, events, data bindings, etc.) applies here too.


Usage

java
// Vertical scroll bar
var scroller = new Scroller.Vertical();
scroller.setRange(0, 100f);
scroller.setValue(50f);
scroller.setOnValueChanged(value -> System.out.println("Scroll: " + value));
parent.addChild(scroller);

// Horizontal scroll bar
var hScroller = new Scroller.Horizontal();
hScroller.setRange(0, 1f);

XML

xml
<!-- Vertical scroller, range 0–100 -->
<scroller-vertical min-value="0" max-value="100" value="0"/>

<!-- Horizontal scroller -->
<scroller-horizontal min-value="0" max-value="1" value="0.5"/>
XML AttributeTypeDescription
min-valuefloatMinimum value. Default: 0.
max-valuefloatMaximum value. Default: 1.
valuefloatInitial value. Default: 0.

Internal Structure

FieldCSS classDescription
headButton.__scroller_head_button__Up/left arrow button.
tailButton.__scroller_tail_button__Down/right arrow button.
scrollContainer.__scroller_scroll_container__Track background.
scrollBar.__scroller_scroll_bar__Draggable thumb button.

Scroller Style

INFO

scroll-delta

Fraction of the total range moved per head/tail button click or mouse-wheel tick.

Default: 0.1 (10 %)

java
scroller.scrollerStyle(style -> style.scrollDelta(0.05f));

INFO

scroll-bar-size

Size of the drag handle as a percentage of the track length. 100 means the handle fills the whole track.

Default: 20 (%)

java
scroller.setScrollBarSize(30f);
// or:
scroller.scrollerStyle(style -> style.scrollBarSize(30f));

Value Binding

Scroller extends BindableUIElement&lt;Float&gt;, so it supports data binding:

java
scroller.bind(DataBindingBuilder.floatVal(
    () -> state.getScrollPosition(),
    v -> state.setScrollPosition(v)
).build());

Fields

NameTypeAccessDescription
headButtonButtonpublic finalUp / left arrow button.
tailButtonButtonpublic finalDown / right arrow button.
scrollContainerUIElementpublic finalThe track element.
scrollBarButtonpublic finalThe draggable thumb.
scrollerStyleScrollerStyleprivate (getter)Current style.
minValuefloatprivate (getter)Minimum of the range.
maxValuefloatprivate (getter)Maximum of the range.
isDraggingbooleanprivate (getter)true while the thumb is being dragged.

Methods

MethodReturnsDescription
setRange(float, float)ScrollerSets [min, max] and clamps current value.
setMinValue(float)ScrollerSets the minimum value.
setMaxValue(float)ScrollerSets the maximum value.
setValue(Float)ScrollerSets the current value (clamped to range).
setOnValueChanged(FloatConsumer)ScrollerRegisters a listener for value changes.
setScrollBarSize(float)ScrollerSets the scroll bar thumb size (0–100 %).
scrollerStyle(Consumer&lt;ScrollerStyle&gt;)ScrollerConfigures style fluently.
getNormalizedValue()floatReturns the current value normalized to [0, 1].
headButton(Consumer&lt;Button&gt;)ScrollerConfigures the head arrow button.
tailButton(Consumer&lt;Button&gt;)ScrollerConfigures the tail arrow button.
scrollContainer(Consumer&lt;UIElement&gt;)ScrollerConfigures the track element.
scrollBar(Consumer&lt;Button&gt;)ScrollerConfigures the drag handle button.

Released under the MIT License.