Using Java applets in HTML
I have an applet that has 2 methods that I want to use. In the first method, I get a list of client printers.
I want to use this printer list in my HTML page in combo. The user has to choose a printer from this combo. After that, the user will click a button to perform some operations. After clicking this button and completing these operations, I want to call the second method of my applet, which receives two parameters: one is a file object, the other is a printer object that is already selected by the user.
My questions:
How can I get a list of printers and use it in my HTML page after the page has loaded? How do I send parameters to the second method of my applet after the button is clicked?
source to share
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>
source to share
Use object id tag in html / jsp
<object id="appletId" type="application/x-java-applet"
and to get the list of printers you have to call the applet method
<script type="text/javascript">
function getPrinters(){
return appletId.getPrinters();
}
</script>
assumed the applet has a public method getPrinters
that returns an array of strings.
source to share