Skip to content

Button

Since 2.2.1

Button is a clickable UI component with a built-in text label, state-reactive textures (normal / hover / pressed), and optional leading or trailing icon decorations.

Internally, Button is a horizontal flex row that owns a single internal TextElement as its label. Because it is a regular UIElement container, you can add extra children alongside the label — most commonly via addPreIcon and addPostIcon.

INFO

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


Usage

java
var button = new Button();
button.setText("my.button.label", true); // translated label
button.setOnClick(event -> {
    // runs on the client when left-clicked
});
button.setOnServerClick(event -> {
    // runs on the server when left-clicked
});
parent.addChild(button);

XML

xml
<!-- Simple button with a translated label -->
<button text="my.button.label"/>

<!-- Empty text attribute hides the label (calls noText()) -->
<button text=""/>

<!-- Style the internal TextElement -->
<button text="my.button.label">
    <internal index="0">
        <!-- TextElement attributes / children go here -->
    </internal>
</button>
XML AttributeTypeDescription
textstringSets the label text. Pass an empty string to hide the text element.

Internal Structure

Button contains one internal element owned by the component:

IndexTypeCSS selectorDescription
0TextElementbutton > text:internalThe label text element

Because the label is internal, it cannot be repositioned or removed through normal child manipulation. You can still target it with LSS selectors:

css
/* Targets only the button's own text, not any text you add as a child */
button > text:internal {
    font-size: 10;
    text-color: #FFFFFF;
}

Text

setText / noText / enableText

Controls the label text displayed inside the button.

java
button.setText("my.translation.key", true);  // translated
button.setText("Literal label", false);       // literal
button.noText();                              // hide the label
button.enableText();                          // show it again

textStyle

Fluently configure the internal TextElement's text rendering:

java
button.textStyle(style -> style
    .textColor(0xFFFFFF)
    .fontSize(9)
    .textShadow(true)
);

Icon Decorations

Add a texture icon before or after the label. Each icon is sized to match the button height and maintains its aspect ratio.

java
button.addPreIcon(Sprites.ICON_WRENCH);    // icon before text
button.addPostIcon(Sprites.ICON_WRENCH);   // icon after text

To create an icon-only button, call noText() together with addPreIcon:

java
new Button()
    .noText()
    .addPreIcon(myIcon)
    .layout(l -> l.size(14));

Button Style

ButtonStyle holds three state-dependent background textures. The active texture changes automatically based on the current interaction state.

INFO

base-background

The texture rendered in the DEFAULT (idle) state, and also when the button is inactive (isActive = false).

Default: Sprites.RECT_RD

java
button.buttonStyle(style -> style.baseTexture(myTexture));
// or:
button.getButtonStyle().baseTexture(myTexture);

INFO

hover-background

The texture rendered in the HOVERED state.

Default: Sprites.RECT_RD_LIGHT

java
button.buttonStyle(style -> style.hoverTexture(myTexture));

INFO

pressed-background

The texture rendered in the PRESSED state (while the mouse button is held down).

Default: Sprites.RECT_RD_DARK

java
button.buttonStyle(style -> style.pressedTexture(myTexture));

Events & Click Handlers

Button provides two convenience setters on top of the standard event system from UIElement:

MethodSideTrigger
setOnClick(UIEventListener)ClientMOUSE_DOWN with left button (button 0)
setOnServerClick(UIEventListener)ServerMOUSE_DOWN with left button (button 0)
java
button.setOnClick(event -> {
    // called on the client immediately on mouse-down
    System.out.println("clicked on client");
});

button.setOnServerClick(event -> {
    // called on the server when the player clicks
    player.sendSystemMessage(Component.literal("clicked!"));
});

INFO

setOnClick fires only for left mouse button clicks (event.button == 0). For other mouse buttons, use addEventListener(UIEvents.MOUSE_DOWN, ...) directly.


State

Button tracks its current visual state automatically:

StateCondition
DEFAULTNo interaction
HOVEREDMouse is over the button
PRESSEDLeft mouse button held down

When isActive is false, the button is always rendered with base-background and ignores mouse input.

You can read the current state in Java / Kotlin via button.getState(). You cannot set it externally — it is managed internally by the component's built-in event listeners.


Fields

Fields specific to Button. See UIElement § Fields for the base fields.

NameTypeAccessDescription
textTextElementpublic finalThe internal label element. Access directly to change text style or content.
buttonStyleButtonStyleprivate (getter)Holds the three state textures.
stateButton.Stateprivate (getter)Current visual state (DEFAULT / HOVERED / PRESSED).

Methods

Methods specific to Button. See UIElement § Methods for the full base API.

MethodReturnsDescription
setText(String, boolean)ButtonSets label text. true = translatable, false = literal.
noText()ButtonHides the internal TextElement.
enableText()ButtonShows the internal TextElement again.
textStyle(Consumer&lt;TextStyle&gt;)ButtonConfigures the internal text style fluently.
addPreIcon(IGuiTexture)ButtonInserts a square icon element before the text.
addPostIcon(IGuiTexture)ButtonAppends a square icon element after the text.
buttonStyle(Consumer&lt;ButtonStyle&gt;)ButtonConfigures ButtonStyle fluently.
getButtonStyle()ButtonStyleReturns the ButtonStyle instance directly.
setOnClick(UIEventListener)ButtonSets the client-side click handler (replaces previous).
setOnServerClick(UIEventListener)ButtonAdds a server-side click listener.
getState()Button.StateReturns the current interaction state.

Released under the MIT License.