Java Swing How to - Change font size with HTML in JEditorPane








Question

We would like to know how to change font size with HTML in JEditorPane.

Answer

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
//from   ww w .j av  a  2  s.co  m
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;

public class Main extends JFrame {
  public static void main(String[] args) {
    Main t = new Main();
    t.setSize(500, 500);
    Container bg = t.getContentPane();
    t.createJEditorPane(bg, bg.getSize());
    t.setVisible(true);
  }

  public void createJEditorPane(Container bg, Dimension size) {
    JEditorPane pane = new JEditorPane();
    pane.setEditable(false);
    HTMLEditorKit editorKit = new HTMLEditorKit();
    pane.setEditorKit(editorKit);
    pane.setSize(size);
    pane.setMinimumSize(size);
    pane.setMaximumSize(size);
    pane.setOpaque(true);
    pane.setText("<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>");
    bg.add(pane, BorderLayout.CENTER);
  }
}