Call a Java Applet from Flex
I'm working on Flex application and I need to open a Java Applet from Flex (e.g. clicking a button). In particular I'd like to open imageJ, a particular imaging program that could work as application, applet or be integrated in a web page. Is there a way to call it from Flex? I've seen a couple of tutorials that explain how to call a single function in another Java file from Flex, but I'm not so sure that i开发者_如何学运维t is what I'm looking for. Thanks for your answers, cheers,
David
I don't know if there's a better way, but if I was doing it, I'd write a JavaScript function that would load the Java applet (could be as simple as document.write("<object …>")
), then use Flex's ExternalInterface to call that JavaScript.
Expose a public method in your applet, which the flex would call. You could load the applet the following way. It is a sample program, to call java methods and get a value from java, you can do changes as per your need
<object
id = "MyApplet"
name = "Some name"
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="0" height="0">
<PARAM NAME = "CODE" VALUE = "com.my.applet.MyApplet.class" >
<PARAM NAME = "CODEBASE" VALUE = "." >
<PARAM NAME = "ARCHIVE" VALUE = "applet-client.jar" >
<PARAM NAME = "cache_option" VALUE="No">
<PARAM NAME = "java_version" VALUE="1.6+">
<param name = "type" value = "application/x-java-applet;version=1.6">
<comment>
<embed
name = "MyApplet"
type = "application/x-java-applet;version=1.6" \
CODE = "com.my.applet.MyApplet.class" \
JAVA_CODEBASE = "." \
ARCHIVE = "applet-client.jar"
cache_option = "No"
scriptable = false
pluginspage = "http://java.sun.com/products/plugin/index.html#download"
width="0" height="0"
>
<noembed>
</noembed>
</embed>
</comment>
</object>
With the above in your html file( I am not explaining everything) , the applet will be downloaded and ready to use. Now on click of a button on your flex app, you should have something like below.
var returnedStringFrom java:String=ExternalInterface.call("document.MyApplet.functionInJava",stringParam);
Note : MyApplet is the name in the object declaration above, the functionInJava is a public function in the the java class com.my.applet.MyApplet. It takes a parameter and returns a string parameter. The Java program will look like below.
package com.my.applet;
public class MyApplet{
//other methods..
public String functionInJava(String stringpm){
// your implementation
return "SomeString";
}
}
Happy coding.
精彩评论