Example usage for javax.swing.text.html HTMLDocument insertAfterEnd

List of usage examples for javax.swing.text.html HTMLDocument insertAfterEnd

Introduction

In this page you can find the example usage for javax.swing.text.html HTMLDocument insertAfterEnd.

Prototype

public void insertAfterEnd(Element elem, String htmlText) throws BadLocationException, IOException 

Source Link

Document

Inserts the HTML specified as a string after the end of the given element.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    JEditorPane edPane = new JEditorPane();
    edPane.setContentType("text/html");

    HTMLEditorKit hek = new HTMLEditorKit();

    edPane.setEditorKit(hek);// www.j av a2  s . c o m

    HTMLDocument doc = (HTMLDocument) edPane.getDocument();

    doc.insertString(0, "Test testing", null);

    Element[] roots = doc.getRootElements();
    Element body = null;
    for (int i = 0; i < roots[0].getElementCount(); i++) {
        Element element = roots[0].getElement(i);
        if (element.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
            body = element;
            break;
        }
    }

    doc.insertAfterEnd(body, "<img src=" + ClassLoader.getSystemResource("thumbnail.png").toString() + ">");
    frame.add(edPane);

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatWritePanel.java

/**
 * Appends the given text to the end of the contained HTML document. This
 * method is used to insert smileys when user selects a smiley from the
 * menu.//from w ww  .  j  a  va  2  s  . co  m
 *
 * @param text the text to append.
 */
public void appendText(String text) {
    HTMLDocument doc = (HTMLDocument) editorPane.getDocument();

    Element currentElement = doc.getCharacterElement(editorPane.getCaretPosition());

    try {
        doc.insertAfterEnd(currentElement, text);
    } catch (BadLocationException e) {
        logger.error("Insert in the HTMLDocument failed.", e);
    } catch (IOException e) {
        logger.error("Insert in the HTMLDocument failed.", e);
    }

    this.editorPane.setCaretPosition(doc.getLength());
}