views:

49

answers:

1

I tried to add Color to java.awt.Font's attributes like this:

font.getAttributes().put(TextAttribute.FOREGROUND, jColorChooser.getColor());

But I get the error

The method put(TextAttribute, capture#12-of ?) in the type Map is not applicable for the arguments (TextAttribute, Color)

The Font API says

This Font only recognizes keys defined in TextAttribute as attributes And FOREGROUND is a present in TextAttribute

Am I doing something wrong?

+3  A: 

In fact, to change a font, you can't change directly its attributes, as Swing fonts are supposed to be immutable.

As a consequence, you have to call its Font#deriveFont(Map) method with a new attribute set. This will create a new font with the given attribute set and, as a consequence, new color.

Riduidel