Create a JEditorPane component to render some HTML output. - Java Swing

Java examples for Swing:JEditorPane

Description

Create a JEditorPane component to render some HTML output.

Demo Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JEditorPane;

import javax.swing.text.BadLocationException;

public class Main {
    /**//from w w  w  .  j  av  a2s .c o  m
     * Create a component to render some HTML output. This is purely here because it took me so long to figure out the
     * minimum magic invocation to make JEditorPane work properly.
     */
    public static JEditorPane createHtmlComponent(InputStream in)
            throws IOException {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        try {
            editorPane.getEditorKit().read(new InputStreamReader(in),
                    editorPane.getDocument(), 0);
        } catch (BadLocationException ex) {
            // should never happen
            throw new RuntimeException(ex);
        }
        return editorPane;
    }

    /**
     * Create a component to render some HTML output. This is purely here because it took me so long to figure out the
     * minimum magic invocation to make JEditorPane work properly.
     */
    public static JEditorPane createHtmlComponent(String html) {
        final JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setText(html);

        return editorPane;
    }
}

Related Tutorials