Example usage for javax.swing.text.html HTMLEditorKit HTMLEditorKit

List of usage examples for javax.swing.text.html HTMLEditorKit HTMLEditorKit

Introduction

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

Prototype

public HTMLEditorKit() 

Source Link

Document

Constructs an HTMLEditorKit, creates a StyleContext, and loads the style sheet.

Usage

From source file:LoadingHTMLDocuments.java

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);/* w  w w. j  a  va  2  s  .c om*/

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

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

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);/*from   w  w  w.  j a  v  a  2 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:Main.java

public static void main(String[] args) {
    JFrame jframe = new JFrame();
    jframe.setSize(500, 200);/*  ww w  . j a  v  a2  s  . c  om*/
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane jTextPane = new JTextPane();
    jTextPane.setEditorKit(new HTMLEditorKit());
    JButton btn = new JButton("Print");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                jTextPane.setContentType("text/html");
                boolean done = jTextPane.print();
                if (done) {
                    System.out.println("Printing is done");
                } else {
                    System.out.println("Error while printing");
                }
            } catch (Exception pex) {
                pex.printStackTrace();
            }
        }
    });
    jframe.add(btn, BorderLayout.SOUTH);
    jframe.add(jTextPane);
    jframe.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URI("http://www.google.com").toURL();
    URLConnection conn = url.openConnection();
    Reader rd = new InputStreamReader(conn.getInputStream());

    EditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    kit.read(rd, doc, 0);//from  www .  jav  a  2 s.c om

    HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
        SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();

        String link = (String) s.getAttribute(HTML.Attribute.HREF);
        if (link != null) {
            System.out.println(link);
        }
        it.next();
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);/*from w w w .  ja v  a 2 s .co m*/

    for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator
            .next()) {

        AttributeSet attributes = iterator.getAttributes();
        String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF);
        System.out.print(srcString);
        int startOffset = iterator.getStartOffset();
        int endOffset = iterator.getEndOffset();
        int length = endOffset - startOffset;
        String text = htmlDoc.getText(startOffset, length);
        System.out.println(" - " + text);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);/*from   w  ww  .  ja v a 2s.c om*/

    for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator
            .next()) {

        AttributeSet attributes = iterator.getAttributes();
        String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF);
        System.out.print(srcString);
        int startOffset = iterator.getStartOffset();
        int endOffset = iterator.getEndOffset();
        int length = endOffset - startOffset;
        String text = htmlDoc.getText(startOffset, length);
        System.out.println(" - " + text);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);//from  w  ww.j a va  2  s. c om

    for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator
            .next()) {

        AttributeSet attributes = iterator.getAttributes();
        String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF);
        System.out.print(srcString);
        int startOffset = iterator.getStartOffset();
        int endOffset = iterator.getEndOffset();
        int length = endOffset - startOffset;
        String text = htmlDoc.getText(startOffset, length);
        System.out.println("  " + text);
    }
}

From source file:DocumentIteratorExample.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);/*from w w  w. j  a v a 2 s .com*/

    for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator
            .next()) {

        AttributeSet attributes = iterator.getAttributes();
        String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF);
        System.out.print(srcString);
        int startOffset = iterator.getStartOffset();
        int endOffset = iterator.getEndOffset();
        int length = endOffset - startOffset;
        String text = htmlDoc.getText(startOffset, length);
        System.out.println(" - " + text);
    }
    System.exit(0);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HTMLDocument doc = new HTMLDocument() {
        public HTMLEditorKit.ParserCallback getReader(int pos) {
            return new HTMLEditorKit.ParserCallback() {
                public void handleText(char[] data, int pos) {
                    System.out.println(data);
                }/* ww w.  j  a  v  a 2  s .  c o m*/
            };
        }
    };

    URL url = new URI("http://www.google.com").toURL();
    URLConnection conn = url.openConnection();
    Reader rd = new InputStreamReader(conn.getInputStream());

    EditorKit kit = new HTMLEditorKit();
    kit.read(rd, doc, 0);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);//from  w w w .  ja v a2 s.  c  o m

    ElementIterator iterator = new ElementIterator(htmlDoc);
    Element element;
    while ((element = iterator.next()) != null) {
        AttributeSet attributes = element.getAttributes();
        Object name = attributes.getAttribute(StyleConstants.NameAttribute);
        if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1)) {
            StringBuffer text = new StringBuffer();
            int count = element.getElementCount();
            for (int i = 0; i < count; i++) {
                Element child = element.getElement(i);
                AttributeSet childAttributes = child.getAttributes();
                if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) {
                    int startOffset = child.getStartOffset();
                    int endOffset = child.getEndOffset();
                    int length = endOffset - startOffset;
                    text.append(htmlDoc.getText(startOffset, length));
                }
            }
            System.out.println(name + ": " + text.toString());
        }
    }
}