Skip to main content

Programmatically invoking a JavaFx Script

3 replies [Last post]
jryog69
Offline
Joined: 2008-02-15
Points: 0

I am running into issues when I try and run the second example in http://jfx.wikia.com/wiki/Programmatically_invoking_a_JavaFX_Script

in the compiled version of JavaFX.

The error I am getting is a NullPointerException error on this line:

InputStreamReader reader = new InputStreamReader(FxScriptLauncher.class.getResourceAsStream("HelloWorld.fx"));

I know it probably has to do with the location of the fx file, but the fx file is in the same directory as java file that I am trying to run.

Any ideas?

jryog

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jryog69
Offline
Joined: 2008-02-15
Points: 0

I think the above code should work.

Thanks.
jryog69

alexsch
Offline
Joined: 2007-07-12
Points: 0

There is a way to execute FX file using full file name ( pack.MyFXFile ):

[code]
public static Object runFXFile(String name) throws Exception {
return runFXFile(name, Thread.currentThread().getContextClassLoader());
}

public static Object runFXFile(String name, ClassLoader classLoader) throws Exception {
try {
Class cls = classLoader.loadClass(name);
//System.out.println("class: \"" + cls + "\"");
Method run = cls.getDeclaredMethod(Entry.entryMethodName(), Sequence.class);
Object args = Sequences.make(String.class, commandLineArgs);
return run.invoke(null, args);
} catch (Throwable e) {
throw new Exception("FX file: \"" + name + "\" was not compiled!!", e);
}
}

[/code]

alexsch
Offline
Joined: 2007-07-12
Points: 0

It is possible to directly instantiate FX objects in Java code.

For example:

[code]
// -------------------------------- MyPanel.fx ------------------------------------
import javafx.ui.*;

public class MyPanel extends CompositeWidget{
public function composeWidget():Widget{
return BorderPanel{
center: Label{ text: "Hello World!"}
}
}
}
// -------------------------------- MyFrame.java ------------------------------------
import javax.swing.JFrame;
import javax.swing.JComponent;

public class MyFrame extends JFrame {

public MyFrame() {
setSize(300, 300);
JComponent myPanel = (new MyPanel()).getComponent();
getContentPane().add(myPanel);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() { new MyFrame().setVisible(true); }
});
}
}
[/code]