Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default font in JEditorPane

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:

enter image description here

like image 751
Sanjeev Avatar asked Sep 22 '12 09:09

Sanjeev


3 Answers

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.

like image 127
Espinosa Avatar answered Oct 20 '22 11:10

Espinosa


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);
like image 23
bobby_light Avatar answered Oct 20 '22 12:10

bobby_light


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.

like image 2
IanB Avatar answered Oct 20 '22 10:10

IanB