Example usage for javax.swing JEditorPane setEditorKitForContentType

List of usage examples for javax.swing JEditorPane setEditorKitForContentType

Introduction

In this page you can find the example usage for javax.swing JEditorPane setEditorKitForContentType.

Prototype

public void setEditorKitForContentType(String type, EditorKit k) 

Source Link

Document

Directly sets the editor kit to use for the given type.

Usage

From source file:net.yacy.cora.util.Html2Image.java

/**
 * render a html page with a JEditorPane, which can do html up to html v 3.2. No CSS supported!
 * @param url//from w w w .  jav a 2s .  c o m
 * @param size
 * @throws IOException 
 */
public static void writeSwingImage(String url, Dimension size, File destination) throws IOException {

    // set up a pane for rendering
    final JEditorPane htmlPane = new JEditorPane();
    htmlPane.setSize(size);
    htmlPane.setEditable(false);
    final HTMLEditorKit kit = new HTMLEditorKit() {

        private static final long serialVersionUID = 1L;

        @Override
        public Document createDefaultDocument() {
            HTMLDocument doc = (HTMLDocument) super.createDefaultDocument();
            doc.setAsynchronousLoadPriority(-1);
            return doc;
        }

        @Override
        public ViewFactory getViewFactory() {
            return new HTMLFactory() {
                @Override
                public View create(Element elem) {
                    View view = super.create(elem);
                    if (view instanceof ImageView) {
                        ((ImageView) view).setLoadsSynchronously(true);
                    }
                    return view;
                }
            };
        }
    };
    htmlPane.setEditorKitForContentType("text/html", kit);
    htmlPane.setContentType("text/html");
    htmlPane.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
        }
    });

    // load the page
    try {
        htmlPane.setPage(url);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // render the page
    Dimension prefSize = htmlPane.getPreferredSize();
    BufferedImage img = new BufferedImage(prefSize.width, htmlPane.getPreferredSize().height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = img.getGraphics();
    htmlPane.setSize(prefSize);
    htmlPane.paint(graphics);
    ImageIO.write(img, destination.getName().endsWith("jpg") ? "jpg" : "png", destination);
}

From source file:storybook.toolkit.swing.SwingUtil.java

public static JTextComponent createTextComponent(MainFrame mainFrame) {
    JTextComponent tc;/*from  ww  w  . j a  v a 2 s  . co m*/
    if (BookUtil.isUseHtmlScenes(mainFrame)) {
        tc = new JEditorPane();
        JEditorPane ep = (JEditorPane) tc;
        ep.setEditorKitForContentType("text/html", new WysiwygHTMLEditorKit());
        ep.setContentType("text/html");
    } else {
        tc = new UndoableTextArea();
        UndoableTextArea ta = (UndoableTextArea) tc;
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        ta.getUndoManager().discardAllEdits();
    }
    return tc;
}