Search code examples
javancursestui

What's the best way to get text user-interfaces (ncurses-like) functionality in Java?


I need to implement a console application (possibly in Java) with ncurses-like functionality (such as navigating a menu and redrawing the whole screen).

The only solutions that I can find to do this so far are CHARVA ("A Java Windowing Toolkit for Text Terminals"), tuipeer ("A Text User Interface for the Java AWT") and a really old Dr. Dobb's article ("A Text UI for the Java AWT ").

So far, CHARVA is the best thing that I can find but I don't like the idea of it using JNI to wrap curses.

Is there any standard way, say with AWT/Swing, to do do this? What other alternatives are there?


Solution

  • Lanterna

    Since 2010 there is Lanterna.

    To quote the project page:

    Lanterna is a Java library allowing you to write easy semi-graphical user interfaces in a text-only environment, very similar to the C library curses but with more functionality. Lanterna is supporting xterm compatible terminals and terminal emulators such as konsole, gnome-terminal, putty, xterm and many more. One of the main benefits of lanterna is that it's not dependent on any native library but runs 100% in pure Java.

    Lanterna is structured into three layers …

    1. The first is a low level terminal interface which gives you the most basic control of the terminal text area. …

    2. The second level is a full screen buffer, the whole text screen in memory and allowing you to write to this before flushing the changes to the actual terminal. …

    3. The third level is a full GUI toolkit with windows, buttons, labels and some other components. …

    When running in a GUI rather than a computer terminal, such as during development with an IDE, a bundled terminal emulator written in Swing will be used rather than standard output.

    See project page at GitHub. Active as of 2024-12. Pure Java. Open-source per LGPL-3.0 license.

    screenshot of example Lanterna app