Styled text in Swing

May 28th, 2004

Just discovered (although it feels that I may have known this before, I've just forgotten about it): how to get colors in your Swing components? Just treat your strings as HTML!

new JTree(new String[]{"<html><font color='red'>O</font><font color='blue'>n</font><font color='green'>e</font></html>", "Two"});

gives you

Now ain't that beautiful ?

1 Response to “Styled text in Swing”

  1. Tom Hawtin Says:
    Unfortunately it's just coincidence if the default font (including size) is the same for both HTML and plain labels.

Leave a Reply