How to set a string's color

188,092

Solution 1

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)

Solution 2

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.

Solution 3

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

Solution 4

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();
  }
}

Solution 5

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

Please refer to the documentation for additional information.

Share:
188,092
Admin
Author by

Admin

Updated on January 17, 2020

Comments

  • Admin
    Admin over 4 years

    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.");
    
  • Admin
    Admin over 15 years
    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?
  • Admin
    Admin over 15 years
    Just tried this. It just prints a black square then 31;1mhello world! in black.
  • Admin
    Admin over 15 years
    I used "result.append("\u001B31;1mhello world!");" instead where result is a JTextArea. Would that make a difference?
  • Admin
    Admin over 15 years
    Can you use this on a JTextArea? (The example you gave using a editor pane)
  • Admin
    Admin over 15 years
    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.
  • Jonik
    Jonik over 15 years
    Doesn't work for me, not inside IDEA, neither on Linux terminal emulator (rxvt), it just prints 1;1mhello world!
  • Nick Fortescue
    Nick Fortescue over 15 years
    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
  • jedierikb
    jedierikb over 15 years
  • Nick Fortescue
    Nick Fortescue over 15 years
    No, you can't if you use JTextArea. If you use JTextPane or JEditorPane you can.
  • msc87
    msc87 about 10 years
    what if we want to print the string in the console?
  • Pankaj Nimgade
    Pankaj Nimgade over 9 years
    but what about windows, i tried this code it doesn't work in command prompt
  • weston
    weston almost 9 years
    It's on googlecode, it's not by google.