JCurses Crash Course
Build colorful, keyboard-driven Java programs that run right in the terminal. Four short lessons, then a quiz to check what stuck.
What JCurses is, and setting it up
JCurses lets a Java program take over a terminal window and draw on it like a canvas. Instead of printing lines that scroll away, you put text at exact spots, in color, and react to single key presses without waiting for Enter.
A terminal is a grid of character cells, usually 80 columns wide and 24 rows tall. Every position you give JCurses is a (column, row) pair on that grid, starting at (0, 0) in the top-left corner.
Drawing mode
Use Toolkit to paint text and read keys yourself.
Widget mode
Use windows, labels, buttons and text fields. Tab moves between them.
Add it to your Eclipse project
- Download JCurses and unzip it.
- Make a
libfolder in your project and copy in two files:jcurses.jarand the native library (.dllon Windows,.soon Linux). - Right-click
jcurses.jar→ Build Path → Add to Build Path. Only the jar, not the native file.
Drawing with Toolkit
Every drawing-mode program follows the same rhythm: clear the screen, print text somewhere, wait for a key, and shut down cleanly. A CharColor is a pair of colors: background first, then text.
import jcurses.system.*;
public class Hello {
public static void main(String[] args) {
CharColor plain = new CharColor(CharColor.BLACK, CharColor.WHITE);
CharColor green = new CharColor(CharColor.BLACK, CharColor.GREEN);
CharColor red = new CharColor(CharColor.BLACK, CharColor.RED);
Toolkit.clearScreen(plain);
// printString(text, column, row, color)
Toolkit.printString("Hello from JCurses!", 5, 2, green);
Toolkit.printString("Press any key (q to quit)", 5, 4, plain);
InputChar key = Toolkit.readCharacter(); // waits for ONE key
Toolkit.printString("You pressed: " + key.getCharacter(), 5, 6, red);
Toolkit.readCharacter();
Toolkit.shutdown(); // always last: restores the terminal
}
}printString("Hi", 5, 2, …)starts 5 columns in and 2 rows down.- Skip
Toolkit.shutdown()and your terminal can be left in a weird state after the program ends.
Running your program
Eclipse's Console panel is not a real terminal, so the green Run button won't work for JCurses. Instead, save in Eclipse (it compiles into the bin folder), open Command Prompt or a terminal in your project folder, and run:
# Windows: entries separated by a semicolon
java -cp bin;lib\jcurses.jar Hello
# Linux / macOS: entries separated by a colon
java -cp bin:lib/jcurses.jar Hello-cpis the classpath: where Java looks for your classes and the jar.- Eclipse's own Terminal view (Window → Show View → Other → Terminal) is a real terminal, so it works too.
UnsatisfiedLinkError? Java couldn't load the native library. Either it's missing from lib, or it's 32-bit while your Java is 64-bit (or the other way around).Windows and buttons
Widget mode works like a tiny version of Swing. Make a Window, attach a layout manager to its root panel, add widgets, then call show().
Window w = new Window(40, 10, true, "My First Window");
DefaultLayoutManager layout = new DefaultLayoutManager();
layout.bindToContainer(w.getRootPanel());
Label msg = new Label("Hello! Press Enter on the button.");
Button b = new Button("Close");
b.addListener(e -> w.close()); // runs when Enter is pressed on it
// addWidget(widget, x, y, width, height, hAlign, vAlign)
layout.addWidget(msg, 0, 1, 38, 1, WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
layout.addWidget(b, 0, 4, 38, 3, WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
w.show();Other widgets to try
TextFieldCheckBoxListPopUpMenuMessageBox