File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How Can I Colorize Text? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Can I Colorize Text?" Watch "How Can I Colorize Text?" New topic

How Can I Colorize Text?

Lazaros Lazarou

Joined: Feb 13, 2001
Posts: 2
I was wondering if anybody knows how can i colorise text.
for example;
class Aclass{
private Label alabel;
public Aclass(){
alabel=new Label();]
alabel.setText("HELLO WORLD");
Is there any way to make the text "HELLO WORLD" colored
before i display it on the label?
thanks in advance.
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
For a label you use alabel.setForeground( );
You can use black, blue, cyan, red, green, gray, darkgray, lightgray, magenta, orange, pink, white or yellow.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
You can use the setForeground() and setBackground() methods on pretty much all of the atomic AWT and Swing components.

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 175
One thing to keep in mind when using the .setBackground(Color) method on Swing components is that by default Swing components are not opaque. This means that they are by default transparent. So if you want to use the setBackground method on Swing a component, you must also make the component non-transparent by using the .setOpaque(true) method. Otherwise your color won't show up.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You can also define more colors than already defined (black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, & yellow) by using RGB (or RGBA) values to construct new colors, like this :

The int values sent to the constructor must be in the range 0 - 255. There is also another constructor that takes float values in the range of 0.0 - 1.0 (to specify percentage of R, G, and B in the color).

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: How Can I Colorize Text?
It's not a secret anymore!