0

i want to create a terminal application in java, but i don't know how to replace text lines that are already printed (kind of "edit the lines that already printed"). \r only returns to the beginning of the last line, and i want to display a 2 dimensional grid. this is a sample for what i want to print:

System.out.println("################");
System.out.println("#--------------#");
System.out.println("#--------------#");
System.out.println("################");


System.out.println("\r################");
System.out.println("#-------X------#");
System.out.println("#--------------#");
System.out.println("################");
Bary12
  • 1,060
  • 9
  • 23

2 Answers2

0

Using System.out.println whatever text is printed to the console is already flushed and cannot be edited. What you can do is you can clear the console and reprint using a fresh set Sys Outs. But for a graphical application I would suggest you have a look at JAVA AWT which will give you functions like paint() and repaint() and other rich UI functions.

For clearing the console you can refer to this: clear console

For JAVA AWT: AWT tutorial

Community
  • 1
  • 1
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • I know to use awt, i just got loved in the terminal when started working with linux. – Bary12 Apr 28 '14 at 07:11
  • Then you can write your own repaint method where you clear the console and reprint the updated output. – Pratik Shelar Apr 28 '14 at 07:14
  • I took a look at other stackoverflow questions, and found this: runtime.getRuntime().exec("cls"); that should clear the terminal. – Bary12 Apr 28 '14 at 07:20
  • It is OS dependent though. it will surely work for windows. Also you can have a look at JLine library as @faisalbhagat suggested. it has methods like clearconsole etc.. – Pratik Shelar Apr 28 '14 at 07:27
0

you should try JLine library. It provides many userful functions for command line applications. http://jline.sourceforge.net/javadoc/

faisalbhagat
  • 2,142
  • 24
  • 27