Skip to main content

Scrolling contents

The achievements menu shows unlocked achievements in three columns, two rows at a time. When the player scrolls down, the button should show which row they are on; at the bottom, it should stop responding.

New achievements keep unlocking. As the list grows, the scrollable range has to grow too.

What Scroll's Signals do​

Scroll.vertical(list, width, rows) accepts a ListSignal directly, and when the list changes, the visible contents and the scrollable range update together.

It provides two Signals. line() is the current row, starting at 0; maxLine() is the furthest row you can scroll to, 0 when the contents fit on one screen. Buttons that depend on them refresh when scrolling or when the list changes.

Achievement list​

Step 1 / 6

Twelve achievements make four rows. Two rows show at a time, so you can scroll to row 2 (counting from 0).

MutableListSignal<String> achievements = ListSignal.of();
achievements.addAll(List.of(
"Stone Age", "Acquire Hardware", "Diamonds!", "Enchanter", "Nether", "Into Fire",
"Eye Spy", "The End?", "Free the End", "Beaconator", "Ocean Monument", "Max Enchant"
));
Scroll<String> scroll = Scroll.vertical(achievements, 3, 2); // 3 columns, 2 rows visible

Item down = Item.builder()
.dependsOn(scroll.line(), scroll.maxLine())
.setItemProvider(context -> {
String text = "Scroll down (row " + (scroll.line().get() + 1) + ")";
return named(Material.ARROW, text);
})
.addClickGuard((item, click) -> scroll.line().get() < scroll.maxLine().get())
.addClickHandler(click -> scroll.advance(1))
.build();

Pane pane = Pane.builder("###MMM###", "###MMM###", "########D")
.addIngredient('M', scroll, name ->
Element.item(Item.simple(named(Material.GOLD_INGOT, name)))
)
.addIngredient('D', down)
.build();
Window.builder(pane).setTitle("Achievements").open(viewer);

achievements.add("Dragon Slayer"); // the list grows, so there is one more row to scroll
Achievements
Stone Age
Acquire Hardware
Diamonds!
Enchanter
Nether
Into Fire
Scroll down (row 1)
scroll.line()0
scroll.maxLine()2
achievements.size()12

For up and down buttons, the up button uses scroll.advance(-1) with the guard scroll.line().get() > 0.

Caveats​

The position belongs to this Scroll

As with Page, the current row lives in the Scroll object. Each player needs their own Scroll to scroll independently; the underlying achievement list can be shared.

Shrinking lists pull the position back

When the list shrinks and the scrollable range gets smaller, the current row falls back into [0, maxLine] automatically. When the contents fit on one screen, you cannot scroll either way.

private static ItemStack named(Material material, String name) {
ItemStack stack = new ItemStack(material);
stack.setData(DataComponentTypes.CUSTOM_NAME,
Component.text(name).decoration(TextDecoration.ITALIC, false)
);
return stack;
}

It uses Paper's DataComponentTypes with Adventure's Component and TextDecoration, matching Item rendering.

NextTab selection stateSwitch tab contents and mark the selected tab.