Share this page 

Bold / Unbold a JLabelTag(s): Swing


JLabel label = new JLabel("I'm bold");
Font font = new Font("Courier", Font.BOLD,12);
label.setFont(font);
You can change the bold attribute after the creation.
Font f = label.getFont();
// bold
label.setFont(f.deriveFont(f.getStyle() | Font.BOLD));

// unbold
label.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD));

// toggle bold
label.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));
Thanks to S.Vespo for the bug fix.