How to change font size with HTML in Java Swing JEditorPane?

Java 8Object Oriented ProgrammingProgramming

Use HTMLEditorKitt to change the font size with HTML. With that, use the JEditorPane setText() method to set HTML:

HTMLEditorKit kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
editorPane.setSize(size);
editorPane.setOpaque(true);
editorPane.setText("<b><font face=\"Verdana\" size=\"30\"> This is a demo text with a different font!</font></b>");

The following is an example to change font size with HTML in Java Swing JEditorPane:

Example

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;
public class SwingDemo extends JFrame {
   public static void main(String[] args) {
      SwingDemo s = new SwingDemo();
      s.setSize(600, 300);
      Container container = s.getContentPane();
      s.demo(container, container.getSize());
      s.setVisible(true);
   }
   public void demo(Container container, Dimension size) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      HTMLEditorKit kit = new HTMLEditorKit();
      editorPane.setEditorKit(kit);
      editorPane.setSize(size);
      editorPane.setOpaque(true);
      editorPane.setText("<b><font face=\"Verdana\" size=\"30\"> This is a demo text with a different font!</font></b>");
      container.add(editorPane, BorderLayout.CENTER);
   }
}

Output

raja
Published on 02-May-2019 10:30:47
Advertisements