Java Swing How to - Change text font for JLabel with HTML








Question

We would like to know how to change text font for JLabel with HTML.

Answer

import javax.swing.JLabel;
import javax.swing.JOptionPane;
/*  w  w w  .  j  ava 2s . c o m*/
public class Main {
  public static void main(String[] args) throws Exception {
    JLabel label = new JLabel("<html><b><font size=+2>"
        + "<center>Hello!<br><i>Press me now!</html>");

    
    JOptionPane.showMessageDialog(null, label);

  }
}