CommandLineRunner hands control to the TUI at startup.
Dependencies
Add the Spring Boot BOM and the required starters to your build. The example application uses Gradle with a version catalog:Integration walkthrough
1
Create the Spring Boot application class
The entry point is a standard
@SpringBootApplication class. Nothing TUI-specific is needed here.SpringExampleApplication.java
2
Launch the TUI from a CommandLineRunner
Implement
CommandLineRunner as a Spring component. Inject your root model bean and start a Program inside run(). Spring calls this after the application context is fully initialized, so all beans are available.Tui4jCommandLineRunner.java
withAltScreen() hides the Spring startup logs behind the TUI and restores the terminal to its original state when the program exits.3
Define a JPA entity and repository
Use standard Spring Data JPA. The example application stores books with authors:
Book.java
BookRepository.java
4
Implement ListDataSource with your JPA repository
ListDataSource is the interface that feeds items into the TUI List component. Inject the JPA repository and implement fetchItems to return a page of results.BookDataSource.java
DefaultItem implementation:BookItem.java
5
Wire everything together in the root model
The root model is a Spring
@Component that receives your BookDataSource and delegate factory by constructor injection. It creates a List component and passes it the data source.MainViewModel.java
6
Configure the data source
For development, H2 in-memory mode requires no external database. Add
application.yml:application.yml
The tui-daemon-combo pattern
Some applications need to run as a background daemon (without a terminal) or as an interactive TUI depending on how they are started. The pattern branches on a CLI flag:main method
Mouse input
Handle clicks and motion in your TUI
Animations with Harmonica
Spring physics for smooth transitions