28

I'm writing a simple console application (80x24) in Java.

Is there a gotoxy(x,y) equivalent for the console?

Lii
  • 11,553
  • 8
  • 64
  • 88
setzamora
  • 3,560
  • 6
  • 34
  • 48

4 Answers4

41

If by gotoxy(x,y), you want to reposition your cursor somewhere specific on the console, you can usually use VT100 control codes to do this. See http://www.termsys.demon.co.uk/vtansi.htm.

Do something like

char escCode = 0x1B;
int row = 10; int column = 10;
System.out.print(String.format("%c[%d;%df",escCode,row,column));

Which should move the cursor to position 10,10 on the console.

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
  • 2
    These are ANSI control codes. http://en.wikipedia.org/wiki/ANSI_escape_code ANSI's even in the end of the link, but I don't blame you for not recognizing it. – Riking Nov 24 '12 at 06:26
  • I think actually this can work even in Windows! Simply run your program in PowerShell instead of the in the regular command prompt. Either start PowerShell and then from there run your program or in the regular command prompt type `powershell` to start PowerShell and then in PowerShell start your program. This might require Windows 10 with the Anniversary Update. – Tmr Oct 10 '16 at 08:30
  • @bmdelacruz doesn't work in my Windows 7 PowerShell :( Did you run it with any option? – Matthieu Mar 09 '18 at 09:36
  • I used Windows 10 Powershell to try it out. I'm sorry, i don't know if i tried to run it with any options. I can't even remember where I used this. – bmdelacruz Mar 09 '18 at 09:47
  • For me it doesn't work on Windows 10 (neither powershell, nor cmd), but it does work on cygwin. – maraswrona Sep 28 '18 at 15:40
  • this works on Windows 10 Version 1803 on the PowerShell and CMD – M. Schena Nov 07 '18 at 20:21
8

I don't think there's a built-in function to do that in Java. There's a Java curses library called JCurses that you can use though.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
4

Not without pulling in a console curses style library...

You can try javacurses and see if that helps you.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
4

I found Lanterna to be a very good library. It does not dependend on any native library but runs 100% in pure Java.

It offers a Screen class which allows text output based on a coordinate system. For OS with a graphical environment it uses a Swing based terminal emulator. Unfortunately, you are not able to force terminal mode on Windows, so if you really need the terminal, use one of the solutions in the other answers.

Lii
  • 11,553
  • 8
  • 64
  • 88
CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • I only found laterna could do the one, very basic thing I neded: `write(char, x, y, foreground, background)`. do you know of a more recent replacement or any alternatives? i'll probably go with laterna but found this topic difficult to research. – oberhamsi Aug 18 '13 at 19:16
  • 1
    @oberhamsi: Use `screen.putString()`. Example: `screen.putString(0, 0, "foo", Color.WHITE, Color.BLACK);` – CodeZombie Aug 27 '13 at 09:17