Perry Smith

Java Questions & Answers

Archive

Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page

Colors in strings
Thursday, 25-Feb-99 12:22:17
207.172.236.130 writes:
1 - how do I display a string in color red (or other)?
2 - same question for a font
3 - how do I display a string with a yellow (or other color) background?

Herve Franceschi

Response
Re: Colors in strings
Thursday, 25-Feb-99 16:42:39
24.1.16.214 writes:

neither strings nor fonts have the capability to be displayed in color.

you have to put the string/font into something derived from Component to display it in the first place, though. a good component to display text with is a Label.

Label inherits six important methods from Component which will allow you to display colored text - getForeground(), setForeground(Color fg), getBackground() and setBackground(Color bg) for managing colors, and getFont() and setFont(Font f) for managing fonts.

you'll end up with something like:

Label foo = new Label (stringVariable);
foo.setForeground(foregroundColorVariable);
foo.setBackground(backgroundColorVarialbe);
foo.setFont(fontVariable);

-calyxa

Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page