Button
Button runs an action when clicked - saving, resetting, or opening another screen - and is the usual Done entry in a screen's footer.
A push button, drawn as the version's vanilla button. Use it for actions, and in the footer for Done.
Button.builder("details")
.label(Text.translatable("mymod.details", "Details..."))
.onPress(() -> host.open(detailsScreen(host)))
.build();
Button.builder("done").label(UiTexts.DONE).onPress(host::back).build();
Builder#
| Method | Required | Default | Notes |
|---|---|---|---|
label(Supplier<Text>) / label(Text) |
yes | the text on the button | |
onPress(Runnable) |
yes | runs on the main thread when clicked | |
enabled, tooltip, visible |
no | options every widget has |
Behaviour#
- After
onPressreturns, the screen refreshes, so labels and enabled states show what the action changed. - Typical actions change your state, write to a StatusLine, or navigate with
host.open,host.backorhost.close. - Slow work (file or network IO) belongs in
host.runAsync, never directly inonPress. - If
onPressthrows, the error is logged and the menu stays open. - Vanilla's convention ends the label of a button that opens a screen with
.... - Footer: yes.
Test it#
host.click("details"); // presses it
Rendered as [button details] Details....
See also#
- Javadoc: Button
- Several screens and dialogs