javajavascripthtmlappletawt

How to call Java applet methods in HTML


I have an applet which has 2 methods that I want to use. On the first method I am getting the client printers list.

I want to use this printer list on my HTML page in a combo. The user is expected to choose a printers from this combo. After that, the user will click a button to do some operations. After clicking that button and finishing those operations, I want to call the second method of my applet, which gets two parameters, one is a file object, other is a printer object that user already selected.

My questions are:

How can I get the printer list and use it on my HTML page after page loads?

How can I send parameters to second method of my applet after clicking the button?


Solution

  • To have my applet work;
    1- I exported the applet as jar file(named as printApplet.jar) and copied it under the same folder as my xhtml page.
    2- I put the applet in xhtml as below;
             <applet id="myApplet"
                code="com.xxx.yyy.console.action.PrintApplet"
                archive="printApplet.jar" width="1" height="1">         
            </applet>
    3- I created a method `enter code here`in the applet which gets printer names as string and has comma(,) between the names. 
    4- I called the applet methods using javascript as below;
    <script type="text/javascript" >
        //<![CDATA[
        function getPrinters() {
            var aplt = document.getElementById("myApplet");     
            var printers = aplt.getPrinterNames();
            var p = printers.split(',');
            var c = document.getElementById("combo");
            for ( var i = 0; i < p.length; i++) {
                var o = document.createElement("option");
                o.text = p[i];
                o.value = i;
                try {
                    c.add(o, null); //Standard 
                } catch (error) {
                    c.add(o); // IE only
                }
            }
        }   
        //]]>
    </script>