Section
Section groups entries under a title, collapsible by default so its children stay hidden until the player expands it, or as a plain heading.
A titled group of entries. A collapsible section (the default) hides its children until the player clicks its header; a non-collapsible one is just a heading above always-visible entries.
Section advanced = Section.builder("advanced")
.title(Text.translatable("mymod.advanced", "Advanced"))
.expanded(config.advancedOpen) // start as the player left it
.onToggle(open -> config.advancedOpen = open) // remember it
.add(Toggle.builder("debug")
.label(Text.translatable("mymod.debug", "Debug output"))
.value(() -> config.debug)
.onChange(v -> config.debug = v)
.build())
.add(Slider.builder("threads")
.label(Text.translatable("mymod.threads", "Threads"))
.range(1, 8).step(1)
.value(() -> config.threads)
.onChange(v -> config.threads = (int) v)
.build())
.build();
MenuScreen.builder("main").title(title).add(advanced).build(); // adds the section and its children
Builder#
| Method | Required | Default | Notes |
|---|---|---|---|
title(Supplier<Text>) / title(Text) |
yes | the title, without the ▼/▶ marker | |
add(Entry) |
no | appends a child; not a section (sections don't nest) | |
add(StringListEditor) |
no | appends a list editor's four entries as children | |
collapsible(boolean) |
no | true |
false makes a plain heading, always expanded |
expanded(boolean) |
no | false (collapsed) |
whether a collapsible section starts expanded |
onToggle(Consumer<Boolean>) |
no | does nothing | called with the new state (true = expanded) on every expand or collapse |
enabled, tooltip, visible |
no | apply to the header; hiding the section hides its children too |
How it is drawn#
- Collapsible: a button reading "▼ title" while expanded and "▶ title" while collapsed. Clicking it expands or collapses the section.
- Not collapsible: a Label with the title.
- The children follow right after the header, and are visible only while the section is visible and expanded.
Rules#
- Add the section to the screen, never its children:
MenuScreen.Builder.add(section)adds both, and adding a child on its own throws. - Sections don't nest, can't be in the footer, and an entry is in at most one section.
- Children's ids share the screen's id space like any other entries. The header uses the section's own id.
- Whether it is expanded lives in the section object; a refresh doesn't change it. Use
expanded(…)andonToggleto keep it across openings.
Methods#
| Method | Does |
|---|---|
setExpanded(boolean) |
expands or collapses and calls onToggle. Nothing happens if it can't collapse or already is in that state. For example, open the section that holds an error. |
toggle() |
flips it, as a click does |
expanded() |
whether the children are shown (if the section is visible) |
children() |
the children in screen order |
Test it#
host.click("advanced"); // expands it
host.slide("threads", 4); // children can be used only while expanded
Rendered as [section advanced] ▼ Advanced, with each visible child on the next lines, indented by two spaces.
See also#
- Javadoc: Section