<?xml version="1.0" encoding="utf-8"?>
<!-- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
-->
<?xml-stylesheet type="text/xsl" href="https://mbien.dev/roller-ui/styles/rss.xsl" media="screen"?><rss version="2.0" 
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
  <title>Michael Bien&apos;s Weblog</title>
  <link>https://mbien.dev/blog/</link>
    <atom:link rel="self" type="application/rss+xml" href="https://mbien.dev/blog/feed/entries/rss?tags=javascript" />
  <description>don&apos;t panic</description>
  <language>en-us</language>
  <copyright>Copyright 2024</copyright>
  <lastBuildDate>Sat, 24 Aug 2024 07:57:58 +0000</lastBuildDate>
  <generator>Apache Roller 6.1.4</generator>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/java_javascript_interoperability_example</guid>
    <title>Java - JavaScript Communication example</title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/java_javascript_interoperability_example</link>
    <pubDate>Tue, 12 May 2009 16:22:57 +0000</pubDate>
    <category>Java</category>
    <category>fun</category>
    <category>java</category>
    <category>javascript</category>
<description>&lt;p&gt;Communication between java applets and javascript code is already available &lt;a href=&quot;http://java.sun.com/products/plugin/1.3/docs/jsobject.html&quot;&gt;since J2SE 1.3&lt;/a&gt; (aka &lt;a href=&quot;http://en.wikipedia.org/wiki/LiveConnect&quot;&gt;LiveConnect&lt;/a&gt;, which was btw. &lt;a href=&quot;http://www.youtube.com/watch?v=4wi9Q1x8j7E&quot;&gt;rewritten from scratch&lt;/a&gt; in Java 6 update 10 as part of the new plugin) and is really easy to implement. It is a simple way to break out of the sandbox and do things which would usually require full system access (a signed applet + user approval via security dialog). For example applets living in a sandbox are only allowed to read mouse events via the AWT/Swing event mechanism which works as long the mouse is over the applet. &lt;/p&gt;&lt;p&gt;To read e.g the mouse position globaly you would need to call &lt;i&gt;MouseInfo.getPointerInfo().getLocation()&lt;/i&gt; which would cause a &lt;i&gt;java.security.AccessControlException: access denied.&lt;/i&gt; However, in javascript it is trivial to track mouse events for the whole html document (e.g google adds track onclick x,y events).&lt;br /&gt;&lt;/p&gt;&lt;p&gt;

All you have to do is to use the object tag instead of applet tag (which is deprecated anyway) and give the object (applet) a name via the id attribute.&lt;/p&gt;&lt;pre style=&quot;border: 1px inset ; margin: 0px; padding: 3px; overflow: auto; text-align: left;&quot; dir=&quot;ltr&quot; class=&quot;alt2&quot;&gt;&amp;lt;&lt;font color=&quot;#ff0000&quot;&gt;&lt;b&gt;form name=&amp;quot;FishForm&amp;quot;&lt;/b&gt;&lt;/font&gt;&amp;gt;   &lt;br /&gt;    &amp;lt;object width=&amp;quot;256&amp;quot; height=&amp;quot;256&amp;quot; type=&amp;quot;application/x-java-applet&amp;quot; &lt;font color=&quot;#ff0000&quot;&gt;&lt;b&gt;id=&amp;quot;CrazyFish&amp;quot;&lt;/b&gt;&lt;/font&gt;&amp;gt;&lt;br /&gt;        &amp;lt;param value=&amp;quot;http://people.fh-landshut.de/~mbien/weblog/java_js_interop/launch.jnlp&amp;quot; name=&amp;quot;jnlp_href&amp;quot; /&amp;gt;&lt;br /&gt;        &amp;lt;param value=&amp;quot;false&amp;quot; name=&amp;quot;draggable&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/object&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&amp;nbsp;now you can simply call methods as usual.
&lt;/p&gt;&lt;pre style=&quot;border: 1px inset ; margin: 0px; padding: 3px; overflow: auto; text-align: left;&quot; dir=&quot;ltr&quot; class=&quot;alt2&quot;&gt;&amp;lt;script language=&amp;quot;JavaScript1.2&amp;quot;&amp;gt;&lt;br /&gt;     //...&lt;br /&gt;     document.onmousemove = onMouseMoved;&lt;br /&gt;&lt;br /&gt;     var tempX = 0;&lt;br /&gt;     var tempY = 0;&lt;br /&gt;&lt;br /&gt;     var applet = document&lt;font color=&quot;#ff0000&quot;&gt;&lt;b&gt;.FishForm.CrazyFish&lt;/b&gt;&lt;/font&gt;;&lt;br /&gt;&lt;br /&gt;     function onMouseMoved(e) {&lt;br /&gt;         //...&lt;br /&gt;         // javascript -&amp;gt; java calls&lt;br /&gt;         applet.jsObjectOrigin(findPosX(applet), findPosY(applet));&lt;br /&gt;         applet.jsMouseMoved(tempX, tempY)&lt;br /&gt;         return true&lt;br /&gt;     }&lt;br /&gt;     //...&lt;br /&gt; &amp;lt;/script&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&amp;nbsp;the other side is a plain old public method implemented in the java applet.&lt;br /&gt;&lt;/p&gt;


&lt;pre style=&quot;border: 1px inset ; margin: 0px; padding: 3px; overflow: auto; text-align: left;&quot; dir=&quot;ltr&quot; class=&quot;alt2&quot;&gt;&amp;nbsp;   /**&lt;br /&gt;     * called from javascript.&lt;br /&gt;     */&lt;br /&gt;    public void jsMouseMoved(int x, int y) {&lt;br /&gt;        //do something usefull&lt;br /&gt;    }&lt;/pre&gt;&lt;p&gt;&amp;nbsp;RIA/Web2.0 Observer Pattern in action ;)
&lt;/p&gt;
    &lt;form name=&quot;FishForm&quot; id=&quot;FishForm&quot;&gt;
        &lt;div id=&quot;FishBanner&quot;&gt;
            &lt;script&gt;
                var attribs = { width:256, height:256, id:&apos;CrazyFish&apos; };
                var params = { jnlp_href:&apos;http://people.fh-landshut.de/~mbien/weblog/java_js_interop/launch.jnlp&apos; };
                var banner0 = new Image(); banner0.src = &quot;//mbien.dev/blog/resource/banner_scripts/play.png&quot;;
                var banner1 = new Image(); banner1.src = &quot;//mbien.dev/blog/resource/banner_scripts/play-active.png&quot;;
            &lt;/script&gt;
	    &lt;a href=&quot;javascript:deployApplet(&apos;FishForm&apos;,&apos;FishBanner&apos;,attribs,params);startEvents();&quot;
                    onmouseover=&quot;play.src=banner1.src;&quot;
                    onmouseout =&quot;play.src=banner0.src;&quot;&gt;
                &lt;img id=&apos;play&apos; src=&quot;//mbien.dev/blog/resource/banner_scripts/play.png&quot; width=&quot;48&quot; height=&quot;48&quot; border=&quot;0&quot; hspace=&quot;100&quot; vspace=&quot;100&quot;/&gt;
            &lt;/a&gt;
        &lt;/div&gt;
    &lt;/form&gt;&lt;p&gt;

        &lt;script language=&quot;JavaScript1.2&quot;&gt;
            &lt;!--
            function startEvents() {
            // Detect if the browser is IE or not.
            // If it is not IE, we assume that the browser is NS.
            var IE = document.all?true:false

            // If NS -- that is, !IE -- then set up for mouse capture
            if (!IE) document.captureEvents(Event.MOUSEMOVE)

            document.onmousemove = onMouseMoved;

            var tempX = 0;
            var tempY = 0;

            var applet = document.FishForm.CrazyFish;

            function onMouseMoved(e) {
                if (IE) { // grab the x-y pos.s if browser is IE
                    tempX = event.clientX + document.body.scrollLeft
                    tempY = event.clientY + document.body.scrollTop
                } else {  // grab the x-y pos.s if browser is NS
                    tempX = e.pageX
                    tempY = e.pageY
                }
                // catch possible negative values in NS4
                if (tempX &lt; 0) {tempX = 0}
                if (tempY &lt; 0) {tempY = 0}

                // javascript -&gt; java calls
                applet.jsObjectOrigin(findPosX(applet), findPosY(applet));
                applet.jsMouseMoved(tempX, tempY)
                return true
            }

            function findPosX(obj) {
                var curleft = 0;
                if (document.getElementById || document.all)  {
                    while (obj.offsetParent) {
                        curleft += obj.offsetLeft
                        obj = obj.offsetParent;
                    }
                }
                else if (document.layers)
                    curleft += obj.x;
                return curleft;
            }

            function findPosY(obj) {
                var curtop = 0;
                if (document.getElementById || document.all) {
                    while (obj.offsetParent)  {
                        curtop += obj.offsetTop
                        obj = obj.offsetParent;
                    }
                }
                else if (document.layers)
                    curtop += obj.y;
                return curtop;
            }
            }
            --&gt;
        &lt;/script&gt;
(The applet won&apos;t work with JRE version &amp;lt; 1.6 update 10 (or the equivalent on Mac OS) since I used the jnlp deployment mechanism, but it wouldn&apos;t have been necessary for this particular applet)&lt;/p&gt;&lt;p&gt;... and never forget Web 2.0 is watching you &lt;br /&gt;&lt;/p&gt;</description>  </item>
</channel>
</rss>