Reputation: 1349
I want to set different font weights for components on my JFrame dialog. How do I do this?
In the below Java statement
setFont(new Font("Dialog", Font.BOLD, 12));
when I use Font.BOLD it is too bold and when I use Font.Plain it is too plain. I want something in-between.
Upvotes: 21
Views: 30421
Reputation: 11078
The solution is to load by name the variant of the font, for example:
Font font = new Font("Segoe UI Semibold", Font.PLAIN, 12);
Upvotes: 3
Reputation: 44413
welle is partially correct. You can use TextAttributes to obtain a font:
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, Font.DIALOG);
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, 12);
label.setFont(Font.getFont(attributes));
A better approach is to derive your font from the font installed on the Swing component by the look-and-feel:
Font font = label.getFont();
font = font.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD));
label.setFont(font);
That will preserve the font's family and size, which users may have set in their desktop preferences for readability reasons.
Upvotes: 22
Reputation: 96
maybe i wrong but i think class Font has only Bold ,plain but you can change after that in number
setFont(new Font("Dialog", Font.BOLD, 12));
setFont(new Font("Dialog", Font.plain, 27));
but in class java.awt.font.TextAttribute
you have WEIGHT_BOLD and WEIGHT_SEMIBOLD ...
Upvotes: 7