Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Can I Colorize Text?

 
Lazaros Lazarou
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Lazaros
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a label you use alabel.setForeground( Color.red );
You can use black, blue, cyan, red, green, gray, darkgray, lightgray, magenta, orange, pink, white or yellow.
Joe
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the setForeground() and setBackground() methods on pretty much all of the atomic AWT and Swing components.
 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
HTH,
-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic