Skip to main content

Calling JavaFX from Java

5 replies [Last post]
javafxdeveloper
Offline
Joined: 2009-09-21
Points: 0

Hello, work since two weeks witch javafx. So i have an java project, now. I want to call from my java project a javafx file. can you help me??? in know how to calling java from javafx but javafx from java???

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

Sorry, the article is pretty old and pretty much everything in JavaFX has changed since then... :-)

Guess it's time for an updated version of that article.

javafxdeveloper
Offline
Joined: 2009-09-21
Points: 0

thx for your help. but can you help me how to works with the interface. i want the text (from my javafx stage) in the frame, so how to use the moo() method in my main function? with this code my programm don´t run and i musst run it as javafx application, not as java application.

FooFX.fx
-------------------------------
class FooFX extends Foo {
override function moo(): Void {
println("moo");

Stage {
title: "foo"
width: 520
height: 300
scene: Scene {
content: [
Text {
font : Font {
size : 16
}
x: 200
y: 20
content: "foo"
}
]
}
}
}
}

Foo.java
---------------------------------
public interface Foo {
void moo();
}

FooJava.java
----------------------------------

public class fooJava {

public static void main(Foo f) {
f.moo();
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);

}

}

javafxdeveloper
Offline
Joined: 2009-09-21
Points: 0

can you show me an example for 1) please

i read the articel for my problem of your blog. i test your example but it doesn´t run, because the line MyLibraryByInterface.Print(o); is wrong. so i have no pounce on the "MyLibraryByInterface" class. can you help me, please?? with your other example 2) and 3) it´s the same. can you upload an example project??

Message was edited by: javafxdeveloper

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

Java Interface:
[code]
public interface Foo {
void moo();
}
[/code]

JavaFX class:
[code]
class FooFX extends Foo {
override function moo(): Void {
println("moo");
}
}
[/code]

Instead of using the JavaFX class FooFX directly, you use the Java interface Foo in your Java code. The only problem with this approach is, that you cannot instantiate FooFX in Java code, but you have to pass it in.

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

The issue here is, that Java does not know JavaFX and has no way to inspect JavaFX classes. There are a number of possibilities to handle this limitation. The most useful ones in my opinion are these, which one fits your need depends on your project.

1) Use Java interfaces. Make the JavaFX classes implement Java interfaces and use the interfaces in your Java code.
2) Use classes in javafx.reflect to access JavaFX (desktop only)
3) Use JSR-223, the Scripting API (desktop only)