How to use custom tag in HTMLDocument in swings JTextPane ?

Discussion in 'Java' started by T. Sander, Jul 12, 2004.

  1. T. Sander

    T. Sander Guest

    Hi !

    I want to use a custom tag "button" in a HTMLDocument displayed by a
    JTextPane.
    The HTML Code looks like :

    <html>
    <head></head>
    <body>
    Start of Text
    <button>Text for Button !!</button>
    End of Text
    </body>
    </html>


    I have two problems with my attached code.
    1) how can I avoid that the text between the button tags is displayed
    plain as text?`
    2) How can I access the text inside the button tags to be displayed on
    the
    JButton. (see method createComponent) ?

    Thanks for any comments

    Thilo


    ###########################
    package test;

    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.text.html.HTMLDocument.*;
    import javax.swing.text.html.HTMLDocument$HTMLReader.*;
    import javax.swing.text.html.HTMLEditorKit.*;

    public class Frame1
    extends JFrame {
    JPanel jPanel1 = new JPanel();
    JTextPane jTextPane1 = new JTextPane();

    public Frame1() {
    try {
    jbInit();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
    frame1.setSize(500, 500);
    frame1.setVisible(true);
    }

    private void jbInit() throws Exception {
    jPanel1.setLayout(new BorderLayout());
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jTextPane1, BorderLayout.CENTER);
    jTextPane1.setContentType("text/html");
    jTextPane1.setEditable(false);
    jTextPane1.setEditorKit(new MyHTMLEditorKit());
    jTextPane1.setText(
    "<html><head></head><body>Start of Text<button>Text for Button
    !!</button>End of Text</body></html>");
    }

    class MyHTMLDocument
    extends HTMLDocument {

    URL base;

    public MyHTMLDocument() {
    super();
    }

    public MyHTMLDocument(StyleSheet styles) {
    super(styles);
    }

    public HTMLEditorKit.ParserCallback getReader(int pos) {
    Object desc = getProperty(Document.StreamDescriptionProperty);
    if (desc instanceof URL) {
    base = (URL) desc;
    }
    MyHTMLReader reader = new MyHTMLReader(pos);
    return reader;
    }

    class MyHTMLReader
    extends HTMLDocument.HTMLReader {

    public MyHTMLReader(int offset) {
    super(offset);
    registerTag(new HTML.UnknownTag("button"), new BlockAction());
    }

    }
    }

    class MyHTMLEditorKit
    extends HTMLEditorKit {

    public MyHTMLEditorKit() {
    super();
    }

    public ViewFactory getViewFactory() {
    return new MyHTMLFactory();
    }

    public Document createDefaultDocument() {
    StyleSheet styles = getStyleSheet();
    MyHTMLDocument doc = new MyHTMLDocument(styles);

    doc.setAsynchronousLoadPriority(4);
    doc.setTokenThreshold(100);
    return doc;
    }

    class MyHTMLFactory
    extends HTMLFactory
    implements
    ViewFactory {
    public MyHTMLFactory() {
    super();

    }

    public View create(javax.swing.text.Element element) {
    HTML.Tag kind = (HTML.Tag) (
    element.getAttributes().getAttribute(
    javax.swing.text.StyleConstants.NameAttribute));

    if (element.getAttributes().getAttribute(HTML.Attribute.ENDTAG)
    != null) {

    if (kind instanceof HTML.UnknownTag &&
    element.getName().equals("button")) {
    return new ComponentView(element) {
    protected Component createComponent() {
    String text =
    "### how to get the text between the button
    tags???? ###";
    JButton button = new JButton("Button : " + text);
    button.setBackground(Color.RED);

    return button;
    }
    };

    }
    }
    return super.create(element);
    }

    }
    }
    }
    T. Sander, Jul 12, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike

    String into HTMLDocument

    Ike, Apr 5, 2004, in forum: Java
    Replies:
    1
    Views:
    9,262
    Rhino
    Apr 5, 2004
  2. Henry S. Thompson

    SPAN tag doesn't display properly in JTextPane

    Henry S. Thompson, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    511
    Henry S. Thompson
    May 1, 2004
  3. Rawash
    Replies:
    1
    Views:
    521
    IchBin
    Dec 10, 2005
  4. shruds
    Replies:
    1
    Views:
    783
    John C. Bollinger
    Jan 27, 2006
  5. Adam Polt
    Replies:
    0
    Views:
    389
    Adam Polt
    May 8, 2007
Loading...

Share This Page