Search code examples
javaswingobjectjbuttonjeditorpane

JButton in a JEditorPane


http://weblogs.java.net/blog/aim/archive/2007/07/embedding_swing.html
http://docs.oracle.com/javase/1.5.0/docs/api/index.html?javax/swing/text/html/ObjectView.html

I add html code to JEditorPane:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <body>
          <table width="90%" height="90%" align="center">
              <tr align="center">
                  <td align="center">
                      <object classid="javax.swing.JButton" label="just do it">
                      </object>
                  </td>
              </tr>
          </table>
      </body>
    </html>

image

How can I control this button? (Change size, color, add listener e.t.c)

I solved the problem, a working example:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.ViewFactory;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.Element;
    import javax.swing.text.html.ObjectView;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.View;
    import javax.swing.text.StyleConstants;

    import javax.swing.text.*;
    import javax.swing.text.html.HTML;
    import javax.swing.text.html.HTMLEditorKit;
    import java.net.URL;

    public class EditorPaneTest extends JFrame
    {
public EditorPaneTest()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationByPlatform(true);        

    JEditorPane editPane = new JEditorPane();
    JScrollPane scrollPane = new JScrollPane(editPane);     

editPane.setContentType("text/html");
editPane.setEditable(false);
    editPane.setEditorKit(new CompEditorKit()); // install our hook

editPane.setText("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"+
"<html>"+
"<body>"+
"<object classid=\"javax.swing.JLabel\" label=\"just do it\">"+
"</object>"+
"</body>"+
"</html>");
    add(scrollPane, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
}

public static void main(String... args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new EditorPaneTest();
        }
    });
}
protected class CompEditorKit extends HTMLEditorKit {
    @Override
    public ViewFactory getViewFactory() {
        return new CompFactory();
    }       
}   
protected class CompFactory extends HTMLEditorKit.HTMLFactory {
    public CompFactory() {
        super();
    }
    @Override
    public View create(Element element) {
        AttributeSet attrs = element.getAttributes();
    Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
    Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
    if (o instanceof HTML.Tag) {       
            if ((HTML.Tag) o == HTML.Tag.OBJECT) {
                return new CompView(element); 
            }
        }
        return super.create(element);
    }
}   
protected class CompView extends ObjectView {
    public CompView(Element element) {
        super(element);
    }
    @Override
    protected Component createComponent() {
        Component component = super.createComponent();  // COMPONENT IS CREATED HERE
    System.out.println(component);
    JLabel layeredPane = (JLabel)component;
    layeredPane.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/9.gif"))));
    return component;   
    }
}
    }

But now another problem. I can not get the Jlabel coordinates System.out.println(layeredPane.getLocation()+ "|"+layeredPane.getSize() +"|"+ layeredPane.getY()); Return 0 0 0


Solution

  • May be this could help http://java-sl.com/custom_tag_html_kit.html

    About coordinates. The JLabel is placed in a container. Size and bounds are set during layout/paint of the pane's content.

    When all content is properly visible you can get the label's parent container and use getBounds() to obtain real bounds.