How to set a string's color
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.
Admin
Updated on January 17, 2020Comments
-
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 over 15 yearsIt'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 over 15 yearsJust tried this. It just prints a black square then 31;1mhello world! in black.
-
Admin over 15 yearsI used "result.append("\u001B31;1mhello world!");" instead where result is a JTextArea. Would that make a difference?
-
Admin over 15 yearsCan you use this on a JTextArea? (The example you gave using a editor pane)
-
Admin over 15 yearsJava 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 over 15 yearsDoesn't work for me, not inside IDEA, neither on Linux terminal emulator (rxvt), it just prints 1;1mhello world!
-
Nick Fortescue over 15 yearsSorry, 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 over 15 years
-
Nick Fortescue over 15 yearsNo, you can't if you use JTextArea. If you use JTextPane or JEditorPane you can.
-
msc87 about 10 yearswhat if we want to print the string in the console?
-
Pankaj Nimgade over 9 yearsbut what about windows, i tried this code it doesn't work in command prompt
-
weston almost 9 yearsIt's on googlecode, it's not by google.