File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Can toString print in color? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can toString print in color?" Watch "Can toString print in color?" New topic

Can toString print in color?

David Tschoepe

Joined: Feb 13, 2005
Posts: 1

I want to spruce up an assignment for class and was wondering if there was a way to make the output from toString to the console display in some color other than white?


Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, toString() doesn't print anything. It simply returns a String representation of the object it is called on. However, OutputStreams and Writers from the package DO print. There may be a way to print in color, if your terminal supports ANSI codes. However, this would take some sophisticated manipulation of the OutputStream. Even more importantly, it seems to me that any attempt to do this would be system dependent.

Anyway, that's my long-winded way of saying that I don't know how to do it. Perhaps I've given you some ideas to think on or keywords you can google for.


Java API Documentation
The Java Tutorial
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
google for curses. it's a time-honored Unix library meant to alleviate some of the platform-dependency of this sort of thing; basically, it's an abstraction library meant to translate between platform-independent, higher-level notions (even some text-mode "GUI" widgets!) and whatever the machine-dependent lower layers need.

as far as i can tell (mostly from this JavaWorld article) you'll likely still need to use JNI to access a curses implementation. but at least you won't have to hardcode ANSI escape sequences in your code, and curses has been ported to Windows, so it's quite widely available.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
curses may have been ported to Windows but it's not generally available on Windows machines.
The results of relying on it are therefore unpredictable.

M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
unless you choose to distribute it with your application. granted that this will bloat your jar files somewhat, but at least it will save you the bother and headache of hand-coding escape sequences.
I agree. Here's the link:
subject: Can toString print in color?
It's not a secret anymore!