• Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating the Console  RSS feed

 
Nura Horne
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am writing a script that polls a data source and just prints some status information from it to the console. Over time the data in the data source will change and I want to update the console accordingly. Updates will be every 10 secs or so. However, I don't want to just keep printing the info with System.out.println as this is ugly and hard for the user to keep tabs on. I want to clear the console and output the info giving the illusion of the screen being updated so all the user sees is the value changing in the output. Is there any way I can do this in Java? Also is it possible to use formatting codes to the console in Java, i.e. to change the colour/boldness of certain characters in the output?
Thanks in advance
Arun
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. I ran into a similar problem and ended up writing something using JNI to control the console on Windows 2000 (win32).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible to do this in a limited, somewhat platform-dependent way on many systems by sending escape sequences to the terminal. You can send ANSI terminal control characters to set the color and do a very limited amount of cursor movement (e.g., printing ASCII 13 will put the terminal cursor at the left margin on the current line, so you can overprint the last thing you printed.)
Something like

will print the word "RED" in red, then one second later, overprint it with GREEN in green. This all works on Linux.
I went and tried this code on Windows: the color part does not work (on Windows 2000, anyway) but the cursor-positioning does indeed work; this may be enough for what you want to do.
[ October 17, 2003: Message edited by: Ernest Friedman-Hill ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!