colors in JLabel

V

vertigo

Hello
Can i set color of text in JLabel ?
If not, how can i make labels with desired colors (background/text) ?

Thanx
 
D

David Rabinowitz

Surround it with html tags such as <font color="red">label</font>. You
can also use css.

David
 
R

Real Gagnon

Can i set color of text in JLabel ?
If not, how can i make labels with desired colors (background/text) ?

The JLabel background is transparent by default, so changing the background
color doesn't seem to do anything. Do something like this :

aJLabel.setOpaque(true);
aJLabel.setBackround(myColor)

Bye.
 
S

Steve Horsley

Tor said:
Have you tried setForeground() and setBackground()?

IIRC, for JLabel(), setBackround() also needs setOpaque(false) before
you can see the result.

Steve
 
A

Andrew Thompson

vertigo said:
Hello
Can i set color of text in JLabel ?
If not, how can i make labels with desired colors (background/text) ?

JLabel l = new JLabel("Stop!");
l.setBackground( Color.RED );
l.setOpaque(true); // very important, default is transparent..
 
A

Andrew Thompson

Andrew Thompson said:
JLabel l = new JLabel("Stop!");
l.setBackground( Color.RED );

And of course, you can use HTML as well..
__________________________
import java.awt.*;
import javax.swing.*;

public class JColoredBG extends JFrame
{
String html =
"<html><body bgcolor=#00ff00>" +
"<p>Go!</p>" +
"</body></html>";

public JColoredBG()
{
super("Colored Labels");

Container c = getContentPane();

c.setLayout(new GridLayout(0,1));

JLabel l1 = new JLabel("Stop!");
l1.setBackground( Color.RED );
// very important, default is transparent..
l1.setOpaque(true);
c.add(l1);

/* Swing text components can render simple
html but load several meg worth of classes
to do so apparently. */
JLabel l2 = new JLabel(html);
c.add(l2);

pack();
setLocation(25,25);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

setVisible(true);
}

public static void main(String args[])
{
new JColoredBG();
}
}
________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top