user633784
user633784

Reputation:

Can we combine 2 font styles together in Java?

I am trying to change the font of a JLabel so it is both BOLD and ITALIC, but it seems there's no static field defined to do so. How can we combine two styles so we can have a bold, italic font?

This code will do it with just bold by using the static field BOLD, but there's no field defined for both bold and italic:

Font font = new Font("Verdana", Font.BOLD, 12);
label = new JLabel ("New Image") ;
label.setFont(font);
label.setForeground(Color.Gray.darker());

Upvotes: 14

Views: 29444

Answers (2)

trashgod
trashgod

Reputation: 205775

Yes, the style parameter is seen as a bitmask:

new Font("Verdana", Font.BOLD | Font.ITALIC, 12)

Upvotes: 31

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

From the API documentation of this constructor:

Parameters:

  • ...
  • style - the style constant for the Font. The style argument is an integer bitmask that may be PLAIN, or a bitwise union of BOLD and/or ITALIC (for example, ITALIC or BOLD|ITALIC). If the style argument does not conform to one of the expected integer bitmasks then the style is set to PLAIN.
  • ...

Thus, use

new Font("Verdana", Font.BOLD | Font.ITALIC, 12);

here.

Upvotes: 12

Related Questions