editorPane.setContentType("text/html");
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");
This does not change the font of the text. I need to know how to set the default font for the JEditorPane with HTML Editor Kit.
Edit:
Try this one:
JEditorPane pane = new JEditorPane();
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
pane.setFont(SOME_FONT);
All credits to de-co-de blogger! Source: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html
I have just tested it. This made JEditorPane to use same font as JLabel
JEditorPane pane = new JEditorPane();
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
pane.setFont(someOrdinaryLabel.getFont());
Works perfectly.
When rendering HTML, JEditorPane's font needs to be updated via its style sheet:
JEditorPane editorPane =
new JEditorPane(new HTMLEditorKit().getContentType(),text);
editorPane.setText(text);
Font font = new Font("Segoe UI", Font.PLAIN, 24));
String bodyRule = "body { font-family: " + font.getFamily() + "; " +
"font-size: " + font.getSize() + "pt; }";
((HTMLDocument)editorPane.getDocument()).getStyleSheet().addRule(bodyRule);
As you are using the HTML toolkit you can set the font in the HTML using standard styling. So change the setText to something like this:
editorPane.setText("<html><head><style>" +
"p {font-family: Segoe UI; font-size:14;}" +
"</style></head>" +
"<body><p>It Works!</p></body></html>");
and remove the setFont statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With