10

I have a JTextPane and I have some text within that JTextPane. However, because I have been using HTML within the Pane, the text seems to have been automatically changed to Times New Roman.

I'm trying to set the font type within the JTextPane to the default font of the GUI (the font of the JTextPane when it's not HTML). However I can't just set the font to one font because it differs from operating system, therefore I want to find a way to get the default font and then change the text I have to the default font.

To demonstrate how the text is swapped to Times New Roman when converted, the following code is the format I have used. How could I change it to achieve my goal?

import javax.swing.JFrame;
import javax.swing.JTextPane;


public class GUIExample {

    public static void main(String[] args) {

        JFrame frame = new JFrame("My App");
        frame.setSize(300,300);
        JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setText("<html><b>This is some text!</b></html>");
        frame.add(pane);

        frame.setVisible(true);

    }

}

Thanks!

mino
  • 6,340
  • 21
  • 55
  • 72

3 Answers3

24

The following will do the trick:

 pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);

(Note that JTextPane extends JEditorPane.)

Update (Aug 2016):

For the setting to survive Look & Feel and system changes (e.g. Fonts changed in the Windows Control Panel) the line can be placed here:

  @Override
  public void updateUI() {
      super.updateUI();
      putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
  }

(This is also called during construction.)

Luke Usherwood
  • 2,850
  • 1
  • 25
  • 33
8

Simplest way is probably something like this:

string fontfamily = pane.getFont().getFamily();

That will give you the default font. Then just apply it using CSS:

pane.setText("<html><body style=\"font-family: " + fontfamily + "\"<b>This is some text!</b></html>");
ChrisPatrick
  • 964
  • 6
  • 18
  • This helped me out with it, I implemented a new method in another class to return the default font for a component and then use that within similar HTML to your own. Thank you. – mino Feb 17 '12 at 22:07
2

The JComponent html renderer uses its own font, and not that of the JComponent. In order to get the component to render the same, you need to set the font attributes in the html string. Among other things, you will need to set the font family, size, bold/italics, etc.

As an example, you could do the following:

JTextPane pane = new JTextPane();
Font font = pane.getFont();
pane.setContentType("text/html");
pane.setText("<html><font face=\"" + font.getFamily() + "\" size=\"" + font.getSize() + "\"></font>This is some text!</html>");

It would be fairly trivial to create a function that does this for you. Pass it a JComponent and a string, and it would create the html text for you, including all the font tags.

Tony
  • 1,311
  • 8
  • 11
  • I tried your method but it didn't seem to work for me, did it exactly how you displayed. – mino Feb 17 '12 at 22:10
  • 2
    is one of those pedantic tags that technically needs to be re-specified inside many other tags - you can't just wrap it around the outside. So this solution is too brittle for general content. – Luke Usherwood Nov 05 '13 at 09:16