Loads the given HTML file to Swing HTMLDocument. - Java Swing

Java examples for Swing:Swing HTML

Description

Loads the given HTML file to Swing HTMLDocument.

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import javax.swing.text.BadLocationException;

import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class Main {
    private static final HTMLEditorKit editorKit = new HTMLEditorKit();

    /**//  w  ww. java2 s . com
     * Loads the given HTML file.
     * 
     * @param htmlFile
     *            HTML file.
     * @return HTML file as HTMLDocument.
     * @throws IOException
     * @throws BadLocationException
     */
    public static HTMLDocument loadDocument(File htmlFile)
            throws IOException, BadLocationException {
        HTMLDocument doc = (HTMLDocument) editorKit.createDefaultDocument();
        FileInputStream fileStream = null;
        try {
            fileStream = new FileInputStream(htmlFile);
            editorKit.read(fileStream, doc, 0);
        } finally {
            if (fileStream != null) {
                fileStream.close();
            }
        }
        return doc;
    }
}

Related Tutorials