Java - Change font in a JTextPane containing HTML
Asked Answered
S

3

12

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!

Senega answered 17/2, 2012 at 21:23 Comment(0)
J
9

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>");
Janitress answered 17/2, 2012 at 21:42 Comment(1)
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.Senega
S
26

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.)

Sealey answered 5/11, 2013 at 9:23 Comment(0)
J
9

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>");
Janitress answered 17/2, 2012 at 21:42 Comment(1)
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.Senega
S
1

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.

Strickle answered 17/2, 2012 at 21:51 Comment(3)
I tried your method but it didn't seem to work for me, did it exactly how you displayed.Senega
<font> 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.Sealey
think font sizes are logical (1-7) not unit point. "</font>" comes to early, else works!Effuse

© 2022 - 2024 — McMap. All rights reserved.