• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How Can I Colorize Text?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the setForeground() and setBackground() methods on pretty much all of the atomic AWT and Swing components.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic