How to set label color (color text) in Java?

How to set label text color?

myLabel.setText("Text Color: Red"); myLabel.??? 

Can I have two separate colors on the same label?

For example:

"Text Color:" should be black and "Red" should be red.

+47
java text fonts colors label
Jun 03 '10 at 13:24
source share
6 answers

Sure. To set the foreground color, simply use label.setForeground(Color.RED) .

For a two-color question . For example, you can use html in the label text:

 frame.add(new JLabel("<html>Text color: <font color='red'>red</font></html>")); 

produces

enter image description here

Another solution, of course, is to use two separate JLabels, each of which has a foreground color.

+102
Jun 03 '10 at 13:28
source share

You can set the color of JLabel by changing the foreground category:

 JLabel title = new JLabel("I love stackoverflow!", JLabel.CENTER); title.setForeground(Color.white); 

As far as I know, the easiest way to create the two-color label you want is to just make two labels and make sure they are next to each other in the correct order.

+40
Jun 03 '10 at 13:27
source share
 JLabel label = new JLabel ("Text Color: Red"); label.setForeground (Color.red); 

it should work

+16
Jun 03 '10 at 13:28
source share
 object.setForeground(Color.green); 

* any color you want * object declared earlier

+6
Mar 22 2018-12-12T00:
source share

Just wanted to add to what @aioobe mentioned above ...

In this approach, you use HTML to color code your text. Although this is one of the most commonly used ways of coloring code for label text, it is not the most efficient way to do it .... given the fact that each label will lead to analysis, rendering, etc. If you have large forms of the user interface to be displayed, every millisecond expects to give a good user interface.

You might want to go through below and try.

Jide OSS (located at https://jide-oss.dev.java.net/ ) is a professional open source library with a really good amount of Swing components ready to use. They have a significantly improved version of JLabel called StyledLabel. This component solves your problem perfectly ... See if their open source licensing applies to your product or not.

This component is very easy to use. If you want to see a demo of your Swing Components, you can run their WebStart demo located at www.jidesoft.com ( http://www.jidesoft.com/products/1.4/jide_demo.jnlp ). All of their suggestions are demos ... and best of all, StyledLabel compares to JLabel (HTML and without) in terms of speed !:-)

A screenshot of the performance can be seen at ( http://img267.imageshack.us/img267/9113/styledlabelperformance.png )

+2
Jun 03 '10 at 16:03
source share

One of the drawbacks of using HTML for labels is when you need to write a localizable program (which should work in several languages). You will have problems changing only the translated text. Or you will have to put all the HTML in your translations, which is very inconvenient, I would even say absurd :)

gui_en.properties:

 title.text=<html>Text color: <font color='red'>red</font></html> 

gui_fr.properties:

 title.text=<html>Couleur du texte: <font color='red'>rouge</font></html> 

gui_ru.properties:

 title.text=<html> : <font color='red'></font></html> 
+2
Nov 17 '13 at 10:31
source share



All Articles