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

假如想象 提交于 2019-11-28 17:54:51
Waldemar Wosiński

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

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.

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/

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
 (...)
 }

}

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.

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.

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.

You can try Jexer - Java Text User Interface:

https://github.com/klamonte/jexer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!