By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

Accessing the DOM API with an Applet (JApplet)

P: n/a
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.
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.