If you are talking about Swing's JLabel or AWT - you can't use ANSI escape codes as you did in console. Instead you have to set the foreground or background color for that JLabel. Something like this:
JLabel label = new JLabel("P" + characterID);
label.setColor(Color.GREEN);
EDIT: If you already have "encoded" strings then you should come up with a regex that will find just the "real" data and ignore escape codes.
Assuming you only have P
, M
and T
prefixes and the characterID
is a decimal number the regex should like this:
String realData = "\\033[32m\\033[44mP111\\033[0m".replace("[^PMT]\\d+", "");
This should remove everything but the data you need.