Sanjeev
Sanjeev

Reputation: 417

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

Upvotes: 17

Views: 16880

Answers (5)

IanB
IanB

Reputation: 449

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.

Upvotes: 2

Espinosa
Espinosa

Reputation: 2623

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.

Upvotes: 34

Canis Majoris
Canis Majoris

Reputation: 1116

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();
        }
    });
}

Upvotes: 1

bobby_light
bobby_light

Reputation: 766

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

Upvotes: 25

Alvin Pradeep
Alvin Pradeep

Reputation: 618

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

Upvotes: 1

Related Questions