Accessing the DOM API with an Applet (JApplet)

From: Kookymon1 (mike_zerby_at_protelinc.com)
Date: 10/29/03


Date: 29 Oct 2003 08:58:22 -0800

This is an attempt to respond to an older question (several months).
Date: 2002-03-07 13:10:23 PST
Subject: On the Common DOM API and Applets.

The original message was:

>LiveConnect and the JSObject technique are Netscape technologies (or
>related to). I want an alternate way of accessing the HTML DOM from
>Java Applets and to call Methods on the Applets from JavaScript.

>Chapters 25 and 26 of the Java Plug-in Developer Guide (JDK 1.4)
>present such alternatives. What got my attention was the Common DOM
>API. "The Common DOM classes allow an application to access the
>underlying DOM of the browser through the APIs in the org.w3c.dom and
>org.w3c.dom.html packages."

>There is an example included:

> DOMService service = null;

> try{
> service = DOMService.getService(MyApplet);
> String title = (String) service.invokeAndWait(
> new DOMAction(){
> public Object run(DOMAccessor accessor)
> {
> HTMLDocument doc = (HTMLDocument) accessor.getDocument();
> return doc.getTitle();
> }});
> } catch (DOMUnsupportedException e1){
> } catch (DOMAccessException e2){
> }

>But there ends the fairy tale. This won't build. getDocument() takes
>an Object arg which is not described in the doc and source code. Then
>the author goes on to describe the com.sun.browser.dom package which
>is in fact the com.sun.java.browser.dom package. And finally, I
>haven't had any success in making it work. I got the same result with
>Mozilla 0.98 and IE 6.0...

>Anyone?

>yours

I don't know what class you have derived from?
But according to the documentation JApplet interface differs from that
of Applet.
So passing "this" to "DOMAccessor.getDocument( Object )" does not
translate correctly.
So instancing the base of Applet from JApplet and then passing it
seems to do the trick.

public class j_applet1 extends javax.swing.JApplet {
    private Applet applet;
    
    /** Initializes the applet j_applet1 */
    public void init() {
        initComponents();
        applet = this;
        
        DOMService service = null;
        try {
            service = DOMService.getService( applet );
            String title = (String) service.invokeAndWait(new
DOMAction() {
                public Object run(DOMAccessor accessor) {
                    // getDocument(Object obj)
                    Object obj = accessor.getDocument( applet );
                    if (obj == null) {
                        return "Object was null";
                    }
                    HTMLDocument doc = (HTMLDocument) obj;
                    if (doc == null) {
                        return "HTMLDocument was null";
                    }
                    return doc.getTitle();
                }
            });
            jTextField1.setText(title + new String("DDDD"));
        }
        catch (DOMUnsupportedException e1) {
            jTextField1.setText("E1" + e1.getMessage());
        }
        catch (DOMAccessException e2) {
            jTextField1.setText("E2" + e2.getMessage());
        }
        catch(Exception e3) {
            jTextField1.setText("E3" + e3.toString());
        }
    }

This seemed to answer the question of why passing a rerfrence of
"this" derived from JApplet fails when passed to methiods expecting an
Applet interface.

This was a tough one.



Relevant Pages

  • Re: Applet Interaction with JS
    ... Applet could interact with client side JavaScript on a browser? ... Does anyone please have a reference as to when one would use the "Common DOM ... API" instead of JSObject? ... Would execution be deferred until the Applet exits or does the JavaScript ...
    (comp.lang.javascript)
  • Re: Mac OS X Issue
    ... >> When the applet is loaded an exception is thrown with the error message ... >> Is there no such definition on jdk for Mac? ... API JavaDocs posted. ... is just which Java is in use on the machine where this error occurred. ...
    (comp.lang.java.help)
  • Re: analytical Skill for Java Development
    ... 5 years ago you probably would have written an Applet which now you write as a Java-Webstart-Application. ... And applet is small API. ... If you drop a ball a foot or two and then from one floor up, it wouldn't be much of a surprise if it broke on the proportionately much higher drop. ...
    (comp.lang.java.programmer)
  • Re: applet & swing
    ... JTextField as soon as an applet loads? ... place the cursor in the name JTextField when the applet first loads. ... API but aren't _in_ the API, if you know what I mean) but it was a pretty ...
    (comp.lang.java.help)
  • Re: passing param to an applet
    ... >>>I'm passing a param to an applet .. ... It does seem to support my first guesses though. ...
    (comp.lang.java.help)