How do I set the colour of a label (coloured text) in Java? How do I set the colour of a label (coloured text) in Java? java java

How do I set the colour of a label (coloured text) in Java?


For single color foreground color

label.setForeground(Color.RED)

For multiple foreground colors in the same label:

(I would probably put two labels next to each other using a GridLayout or something, but here goes...)

You could use html in your label text as follows:

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

which produces:

enter image description here


You can set the color of a JLabel by altering the foreground category:

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

As far as I know, the simplest way to create the two-color label you want is to simply make two labels, and make sure they get placed next to each other in the proper order.


JLabel label = new JLabel ("Text Color: Red");label.setForeground (Color.red);

this should work