title: How can I create a Java/Swing text component that is both styled and has a custom font?
body: <p>How can I create a Java/Swing text component that is both styled and has a custom font? I want to highlight a small portion of the text in red, but at the same time use a custom (embedded via <code>Font.createFont</code>) font. JLabel accepts HTML text, which allows me to highlight a portion of the text, but it ignores font settings when rendering HTML. Other text components such as JTextArea will use the custom font, but they won't render HTML. What's the easiest way to do both?</p>
  
  <p>Here's an example of using JTextPane unsuccessfully:</p>
  
  <pre><code>    JTextPane textPane = new JTextPane();
      textPane.setFont(myCustomFont);
      textPane.setText(text);
      MutableAttributeSet attributes = new SimpleAttributeSet();
      StyleConstants.setForeground(attributes, Color.RED);
      textPane.getStyledDocument().setCharacterAttributes(
          text.indexOf(toHighlight),
          toHighlight.length(),
          attributes, true
      );
  </code></pre>
  
  <p>This successfully displays the text with the "toHighlight" portion highlighted in red, but it doesn't use myCustomFont. Note that I could set a String font with <code>StyleConstants.setFontFamily()</code>, but not a custom font.</p>