Display html document with JEditorPane in Java

Description

The following code shows how to display html document with JEditorPane.

Example


 // w w  w. j  av  a  2  s  . c o m

import java.awt.BorderLayout;
import java.io.FileReader;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLEditorKit;

public class Main {
  public static void main(String args[])throws Exception {
    JFrame frame = new JFrame("Tab Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditorKit(new HTMLEditorKit());
    
    String filename = "yourFile.htm";
    
    FileReader reader = new FileReader(filename);
    editorPane.read(reader, filename);

    JScrollPane scrollPane = new JScrollPane(editorPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer