Skip to main content
This guide walks through building a small interactive coffee-ordering menu — the canonical TUI4J demo. By the end you will understand the three methods every TUI4J app implements and how to run it with Program. The complete source is in the examples directory.

The Elm Architecture

TUI4J is built on The Elm Architecture. Your application has one Model that owns all state, and three methods that drive the runtime loop:
  • init() — returns an optional startup command (e.g., start a ticker, make an HTTP request)
  • update(Message) — receives events (keypresses, timers, responses) and returns the next model state
  • view() — returns a string that TUI4J renders to the terminal

Build the coffee demo

1

Define the model

Create a class that implements Model. The model holds your application state — in this case, the cursor position and the selected item.
SELECTION is a Lipgloss style that highlights the active row with a terminal color. CHOICES is the fixed list of menu options.
2

Implement init()

Return null from init() to indicate there is no startup command. The program starts immediately without waiting for any I/O.
3

Implement update(Message)

update receives every event. Pattern-match on KeyPressMessage to handle keyboard input, then return UpdateResult.from(...) with the next model state and an optional command.
QuitMessage::new is a command supplier that tells TUI4J to exit the program after the current update cycle.
4

Implement view()

view returns a string. TUI4J re-renders this string to the terminal after every update call, automatically clearing the previous output.
When the cursor is on a row, SELECTION.render(...) applies the Lipgloss style. Other rows are rendered as plain strings.
5

Run with Program

Create a Program with your model and call run(). After the program exits, you can read state back from the model directly.
program.run() blocks until the user quits. Because demoModel is a reference to the same object the runtime uses, its final state is available after run() returns.

Full source

Here is the complete Demo.java file, ready to compile:

Next steps

Components

Explore the full Bubbles library: lists, text inputs, spinners, tables, progress bars, and more.

More examples

Browse the examples directory for spinners, autocomplete, file pickers, tabs, and Spring Boot integration.

Styling with Lipgloss

Learn how to apply colors, borders, padding, and layout joins to your views.

Messages and commands

Understand how to send messages between components and trigger side effects with commands.