Occasionally the requirement arises to call a JavaFX function from within a Java method. I needed to do this yesterday, so I thought I'd take a moment to explain a way to do this in three simple steps:
- Create a Java interface
- Extend the interface with a JavaFX class, implementing any functions defined by the interface
- Obtain a reference to, and call a function of, the JavaFX class defined by the interface
Here are snippets of the code that I created yesterday in the context of the steps listed above:
Create the Java interface
public interface UniverseHandler {
public void onSphereClicked(SphereBranch sphereBranch);
}
Extend the interface with a JavaFX class, implementing any functions defined by the interface
public class WindshieldModel extends UniverseHandler {
...model code omitted...
/**
* This is called when user clicks a sphere on the 3D Canvas
*/
public override function onSphereClicked(sphereBranch:SphereBranch) {
curSphereBranch = sphereBranch;
}
}
Obtain a reference to, and call a function of, the JavaFX class defined by the interface
universeHandler.onSphereClicked(currentSphereBranch);
The universeHandler was passed into the constructor of the Java class.
Thanks again to InteractiveMesh.org for their lightweight 3D canvas and classes that enable JavaFX/Java 3D integration!
Regards,
Jim Weaver
JavaFXpert.com
"hi i created image transition demo in JavaFX u can see it here it take some time to load because i upload html file on one server and JNLP and jar file on other server on my server it does not allow JNLP files."
Farrukh Obaid,
Sweet! Have you thought about putting it on jfxstudio.org and showing the source code?
Thanks,
Jim Weaver
Posted by: Jim Weaver | April 16, 2009 at 11:13 AM
Alejandro,
Have you tested calling methodA from IA of an object CFX?
You could be creating new methods and not ones which override the interface methods.
Posted by: Pedro Duque Vieira | April 13, 2009 at 02:44 PM
hi i created image transition demo in JavaFX u can see it here it take some time to load because i upload html file on one server and JNLP and jar file on other server on my server it doesnot allow JNLP files..
http://farrukh.hostrator.com/
Posted by: Farrukh Obaid | April 12, 2009 at 05:39 PM
Finally I found the solution, but I can't explain why, I think it's a bug too. Just type the keyword "override" for the first level of classes / interfaces in your hierarchy. For example:
public interface IA{
public void methodA();
}
public interface IB extends IA{
public void methodB();
}
public interface IC extends IB{
public void methodC();
}
public class CFX extends IC{
public function methodA(){}
public function methodB(){}
public override function methodC(){}//<--override only for IC
}
Regards
Posted by: Alejandro | April 12, 2009 at 01:46 AM