Setting default font in JEditorPane
Asked Answered
M

5

17
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

Matta answered 22/9, 2012 at 9:51 Comment(2)
Please post your code in text format and not an image of it, Because anyone who wants to test it has to write it out. This isn't school :)Firebrat
More on how to take a screenshot.Wiggins
S
32

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.

Shivers answered 2/3, 2013 at 22:24 Comment(0)
K
23

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);
Kook answered 22/9, 2012 at 14:33 Comment(0)
T
2

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.

Trigraph answered 28/9, 2017 at 14:38 Comment(0)
S
1

I've checked your code, there shouldn't be any problem. Have you tested other fonts? Please try "Segoe Script" font and see if it changes.

Edit: I have tried the code bellow, it works fine for me. Are you sure the code you've posted is exactly the same as you've implemented?

    editorPane.setContentType("text/html");
    editorPane.setFont(new Font("Segoe Script", 0, 14));
    editorPane.setText("it works!");

Edit2: Change your main method as follow. It sets the Nimbus LookAndFeel. I haven't checked other LookAndFeels yet.

public static void main(String[] args)
{
    try
    {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
        {
            if ("Nimbus".equals(info.getName()))
            {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex)
    {
        java.util.logging.Logger.getLogger(EditorPaneDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            new EditorPaneDemo();
        }
    });
}
Serotherapy answered 22/9, 2012 at 10:18 Comment(3)
as I mentioned in my edited answer make sure you've implemented the exact codeSerotherapy
please observe the uploaded image, as the text is not of the font segoe script.Matta
Maybe it is not supported on your platform. What's your operating system? See my post again, I showed you how to set Nimbus LookAndFeel. Try that, it must work.Serotherapy
C
1

try below

editorPane.setFont(new Font("Segoe UI", Font.PLAIN, 24));

below is working code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class jeditorfont extends JFrame {
  private JTextPane textPane = new JTextPane();

  public jeditorfont() {
    super();
    setSize(300, 200);

    textPane.setFont(new Font("Segoe UI", Font.PLAIN, 24));

    // create some handy attribute sets
    SimpleAttributeSet red = new SimpleAttributeSet();
    StyleConstants.setForeground(red, Color.red);
    StyleConstants.setBold(red, true);
    SimpleAttributeSet blue = new SimpleAttributeSet();
    StyleConstants.setForeground(blue, Color.blue);
    SimpleAttributeSet italic = new SimpleAttributeSet();
    StyleConstants.setItalic(italic, true);
    StyleConstants.setForeground(italic, Color.orange);

    // add the text
    append("NULL ", null);
    append("Blue", blue);
    append("italic", italic);
    append("red", red);

    Container content = getContentPane();
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {
      d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
  }

  public static void main(String[] args) {
    new jeditorfont().setVisible(true);
  }
}

ref: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetFontFontfont.htm

Culpable answered 22/9, 2012 at 10:52 Comment(1)
Nice but this is an example of JTextPaneFirebrat

© 2022 - 2024 — McMap. All rights reserved.