26

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Anthony
  • 410
  • 1
  • 4
  • 8
  • I looked into this a couple of years ago and didn't find any more than you have. I suspect the problem is that the low-level terminal functionality is highly platform-specific -- and not just at the OS level but at the terminal emulator level (VT100, xterm, etc). I'm not sure how I'd even go about dealing with that, without wrapping a native API like curses (or termcap / terminfo, at least) that already abstracts it. I suppose you could talk raw control characters to `System.out`. – David Moles Aug 24 '09 at 15:22
  • Talking raw control chars to stdout is exactly how curses does it, so it's a sensible idea, although a substantial piece of work. – skaffman Aug 24 '09 at 19:02
  • 2
    What's wrong with JNI? I'd go with CHARVA, it looks complete (ish). –  Aug 25 '09 at 19:05
  • I would recommend going with CHARVA. There is a lot going on in the ncurses library. Not only does it support lots of different terminal types, it also handles lots of the cases where a particular version of an emulator is buggy. The only sane ways to get ncurses-equivalent terminal support are to wrap ncurses as CHARVA does, or to port ncurses to Java. The latter would be cool if someone would support it going forward. – Devon_C_Miller Aug 28 '09 at 16:26
  • possible duplicate of [What's a good Java, curses-like, library for terminal applications?](http://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications) – rath Apr 25 '14 at 12:01

8 Answers8

30

Since 2010 there is Lanterna :

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.

More here: https://github.com/mabe02/lanterna

Czipperz
  • 3,268
  • 2
  • 18
  • 25
Waldemar Wosiński
  • 1,490
  • 17
  • 28
  • 1
    That looks great. I don't work at the company where I needed that, but now I know for future reference :) Thanks. – Anthony Mar 25 '13 at 11:03
10

You may want to go vote for this issue here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351276

Essentially there is no good way to get ncurses-like functionality without JNI until this issue is addressed.

stinkymatt
  • 1,658
  • 11
  • 14
3

You can try Jexer - Java Text User Interface:

https://jexer.sourceforge.io/

https://gitlab.com/AutumnMeowMeow/jexer

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
2

The short answer is deal with a Java wrapper around curses.

The long answer:

Terminals vary a lot, which is why terminfo/termcap libraries exist and why they are a mess to deal with (The maintainers of those projects are saints btw). They abstract away all the really basic terminal variations to something sane. Curses make those in nice efficient libraries to use.

If you want a pure Java solution, you'll need both those libraries or their equivalent in Java. I'm sure someone will point you to that if it exists, but I as far as I know it doesn't exist.

D'Nabre
  • 2,226
  • 16
  • 13
2

I'm using JavaTUI (http://sourceforge.net/projects/javatui/files/) in my several console java projects. It's best what i can find but it so far from perfect. I'm think there is not a good TUI implemetation in java world.

Alexey Sviridov
  • 3,360
  • 28
  • 33
1

I believe Jcurses is a native java implementation of the curses API, I recall it have a few quirks, but it should be able to do what you want:

http://sourceforge.net/projects/javacurses/

1

Try java curses (sorry it uses JNI). I also tried to implement a short version of this library just to learn JNI , see http://plindenbaum.blogspot.com/2008/01/java-native-interface-jni-notebook.html. One could also imagine a specialized JPanel displaying a matrix of characters:

public class TPanel extends JPanel
{
private Vector<Vector<YourCharAndStyle>> rows;

protected void paintComponent(Graphics g)
 {
 //paint the characters
 (...)
 }

}
Pierre
  • 34,472
  • 31
  • 113
  • 192
1

I think it would be better to abstract your Java code from the TUI and use ncurses against several separated parts of your application or using arguments, in a web-services style. For example, code your TUI and when the user calls an action, use ncurses to call your code passing some parameters

java -Daction=doSomething MyApp

This way you can code your app using a GUI also in case you need to.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63