16

Does anyone know how I would set the color of a string that will be printed using System.out?
This is the code I currently have:

System.out.println("TEXT THAT NEEDS TO BE A DIFFERENT COLOR.");
Universal Electricity
  • 775
  • 1
  • 12
  • 26
  • Strings do not have color information. Please specify what you are trying to do. Is it a Swing application? Or do you need ANSI-color on a terminal screen? – Rolf Feb 19 '09 at 13:14
  • It's Just plain text. I have a list of strings with an assigned number. What I am trying to do is set the colour of the string based on that number, so if the number is 2, then set the colour of that string to red before it prints to screen. –  Feb 19 '09 at 13:19
  • it's still not clear where you want to print the string – basszero Feb 19 '09 at 14:33
  • 4
    I'm pretty sure they want to send it to the console. – Tim Frey Feb 19 '09 at 14:39
  • 2
    Instead of closing this question I would recommend changing the title/content. This is the third hit when Googling "java print red color console". – Christophe De Troyer Jun 25 '14 at 22:21

9 Answers9

23

Console

See the Wikipedia page on ANSI escapes for the full collection of sequences, including the colors.

But for one simple example (Printing in red) in Java (as you tagged this as Java) do:

System.out.println("\u001B31;1mhello world!");

The 3 indicates change color, the first 1 indicates red (green would be 2) and the second 1 indicates do it in "bright" mode.

GUI

However, if you want to print to a GUI the easiest way is to use html:

JEditorPane pane = new new JEditorPane();
pane.setText("<html><font color=\"red\">hello world!</font></html>");

For more details on this sort of thing, see the Swing Tutorial. It is also possible by using styles in a JTextPane. Here is a helpful example of code to do this easily with a JTextPane (added from helpful comment).

JTextArea is a single coloured Text component, as described here. It can only display in one color. You can set the color for the whole JTextArea like this:

JTextArea area = new JTextArea("hello world");
area.setForeground(Color.red)
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
  • 1
    Just tried this. It just prints a black square then 31;1mhello world! in black. –  Feb 19 '09 at 14:14
  • I used "result.append("\u001B31;1mhello world!");" instead where result is a JTextArea. Would that make a difference? –  Feb 19 '09 at 14:16
  • 1
    This code was for output to a text window, and on a Windows/DOS machine would only work if ANSI.sys installed. I've just added a swing method, as you are using a GUI – Nick Fortescue Feb 19 '09 at 14:20
  • Can you use this on a JTextArea? (The example you gave using a editor pane) –  Feb 19 '09 at 14:21
  • Java doesn't like the term 'red' in your gui example because it is not in the quotes and it doesn't recognise that term like that. –  Feb 19 '09 at 14:24
  • Doesn't work for me, not inside IDEA, neither on Linux terminal emulator (rxvt), it just prints 1;1mhello world! – Jonik Feb 19 '09 at 14:25
  • Sorry, can't do a JTextArea in more than one color as now described in the main text. You'll have to switch to JTextPane or JEditorPane, but they aren't too different, and the tutorial is pretty good – Nick Fortescue Feb 19 '09 at 14:28
  • So you can't actually set the colour of the text before printing it to the Text area? because I don't want to set the colour of the backgroud, I want to set the colour of the text(String) before it is printed to the screen. –  Feb 19 '09 at 14:28
  • http://www.java2s.com/Code/Java/Swing-JFC/ExtensionofJTextPanethatallowstheusertoeasilyappendcoloredtexttothedocument.htm – jedierikb Feb 19 '09 at 14:29
  • No, you can't if you use JTextArea. If you use JTextPane or JEditorPane you can. – Nick Fortescue Feb 19 '09 at 14:33
  • what if we want to print the string in the console? – msc87 Apr 08 '14 at 13:15
12

for linux (bash) following code works for me:

System.out.print("\033[31mERROR  \033[0m");

the \033[31m will switch the color to red and \033[0m will switch it back to normal.

Christian
  • 3,551
  • 1
  • 28
  • 24
4

Google aparently has a library for this sort of thing: http://code.google.com/p/jlibs/wiki/AnsiColoring

There's also a Javaworld article on this which solves your problem: http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html

Rolf
  • 7,098
  • 5
  • 38
  • 55
1

Download jansi-1.4.jar and Set classpath and Try This code 100% working :

import org.fusesource.jansi.AnsiConsole;
import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;

public class SampleColour
{
  public static void main(String[] args)
  {
    AnsiConsole.systemInstall();

    System.out.println(ansi().fg(RED).a("Hello World").reset());
    System.out.println("My Name is Raman");

    AnsiConsole.systemUninstall();
  }
}
Raman
  • 145
  • 1
  • 5
1

setColor(). Assuming you use Graphics g in an AWT context.

Please refer to the documentation for additional information.

TomHastjarjanto
  • 5,386
  • 1
  • 29
  • 41
1

If you're printing to stdout, it depends on the terminal you're printing to. You can use ansi escape codes on xterms and other similar terminal emulators. Here's a bash code snippet that will print all 255 colors supported by xterm, putty and Konsole:

 for ((i=0;i<256;i++)); do echo -en "\e[38;5;"$i"m"$i" "; done

You can use these escape codes in any programming language. It's better to rely on a library that will decide which codes to use depending on architecture and the content of the TERM environment variable.

heeen
  • 4,736
  • 2
  • 21
  • 24
1

I created an API called JCDP, former JPrinter, which stands for Java Colored Debug Printer. For Linux it uses the ANSI escape codes that WhiteFang mentioned, but abstracts them using words instead of codes which is much more intuitive. For Windows it actually includes the JAnsi library but creates an abstraction layer over it, maintaining the intuitive and simple interface created for Linux.

This library is licensed under the MIT License so feel free to use it.

Have a look at JCDP's github repository.

dialex
  • 2,706
  • 8
  • 44
  • 74
0

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation).

As per your question, it is not clear what actually you want. But you can store color information to a string variable. Are you thinking of setting the color in a string variable?

Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50
Jim
  • 3,110
  • 4
  • 31
  • 38
0
public class colorString
{

public static void main( String[] args )
{
    new colorString();   

}

public colorString( )
{
    kFrame f = new kFrame();
    f.setSize( 400, 400 );
    f.setVisible( true );
}

private static class kFrame extends JFrame
{
    @Override
    public void paint(Graphics g) 
    {
        super.paint( g );
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor( new Color(255, 0, 0) );
        g2d.drawString("red red red red red", 100, 100 );
    }
}
}
jedierikb
  • 12,752
  • 22
  • 95
  • 166
  • It's a nice Idea but is it possible to get it to set the String's colour in the Jframe I have already created (Instead of in a new JFrame) as the class I am working on is a gui and I want to colour the text and print it to a JTextArea? –  Feb 19 '09 at 13:55