title: JLabel html text ignores setFont
body: <p>I've just started porting my Swing app from OS X to Windows and things are painful with <code>JLabel</code>s.</p>
  
  <p>I've noticed that the font specified to <code>setFont</code> is ignored if the label's text is HTML (this doesn't happen on the Mac).  The HTML formatting is EXTREMELY useful for readability on complicated displays.</p>
  
  <p>Under normal circumstances I'd specify the font in an HTML tag, but the font I'm using is loaded at runtime using <code>Font.createFont</code> with a ttf out of the JAR.  I tried using the loaded font's name in the font tag, but that didn't work.</p>
  
  <p>Is there any way I can use a loaded <code>awt.Font</code> with an html-ified <code>JLabel</code> on Windows?</p>
  
  <p>Here's an example.  I can't share my application's font, but I just ran it with this one (a pure TTF) and the same behavior happens:</p>
  
  <p><a href="http://www.dafont.com/sophomore-yearbook.font" rel="nofollow noreferrer">http://www.dafont.com/sophomore-yearbook.font</a></p>
  
  <pre><code>import java.awt.Font;
  import java.io.File;
  import javax.swing.*;
  
  public class LabelTestFrame extends JFrame {
  
          public LabelTestFrame() throws Exception {
                  boolean useHtml = true;
                  String fontPath = "C:\\test\\test_font.ttf";
                  JLabel testLabel = new JLabel();
                  Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f);
                  testLabel.setFont(testFont);
                  if (useHtml) testLabel.setText("&lt;html&gt;Some HTML'd text&lt;/html&gt;");
                  else testLabel.setText("Some plaintext");
                  getContentPane().add(testLabel);
                  setSize(300,300);
          }
  
          public static void main(String[] args) {
                  SwingUtilities.invokeLater(new Runnable() {
                          @Override
                          public void run() {
                                  try {new LabelTestFrame().setVisible(true);}
                                  catch (Exception e) {e.printStackTrace();}
                          }
                  });
          }
  
  }
  </code></pre>
  
  <p>EDIT: interestingly enough, if I use one of the ttf's from the JRE's lib/fonts folder (in this case one of the Lucida fonts here renamed to test_java.ttf) this snippet produces identical results with the boolean on and off.</p>
  
  <pre><code>public LabelTestFrame() throws Exception {
      boolean useHtml = false;
      String fontPath = "C:\\test\\test_java.ttf";
      JLabel testLabel = new JLabel();
      Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f);
      testLabel.setFont(testFont);
      if (useHtml) testLabel.setText("&lt;html&gt;&lt;b&gt;Some HTML'd text&lt;/b&gt;&lt;/html&gt;");
      else testLabel.setText("Some plaintext");
      getContentPane().add(testLabel);
      setSize(300,300);
  }
  
  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              try {new LabelTestFrame().setVisible(true);}
              catch (Exception e) {e.printStackTrace();}
          }
      });
  }
  </code></pre>
  
  <p>EDIT 2:  The method described here for setting the default JLabel font has exactly the same problem (plaintext shows fine, html'd text doesn't):  <a href="https://stackoverflow.com/questions/1966296/changing-default-jlabel-font">Changing default JLabel font</a></p>
  
  <p>EDIT 3:  I've noticed that even random fonts from dafont will work if they're installed on the system (even with this exact code, where I'm loaded a copy of the [now installed] ttf from a file).</p>