JavaFX FXMLLoader Problem
1011660May 29 2013 — edited May 30 2013I develop an OpenOffice addon which uses JavaFX for UI.
Code as in "JavaFX in Swing" example works fine (UI elements are created using java code).
But once I try to load UI from FXML file I face following problem:
Line on which problem happens:
parent = FXMLLoader.load(getClass().getResource("LoginDialog.fxml"));
Stack trace:
java.lang.ExceptionInInitializerError
at javafx.fxml.JavaFXBuilderFactory.<init>(JavaFXBuilderFactory.java:53)
at javafxswingapplication1.JavaFXSwingApplication1.createScene(JavaFXSwingApplication1.java:82)
at javafxswingapplication1.JavaFXSwingApplication1.access$000(JavaFXSwingApplication1.java:30)
at javafxswingapplication1.JavaFXSwingApplication1$2.run(JavaFXSwingApplication1.java:74)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:1689)
... 12 more
Exception in runnable
java.lang.NullPointerException
The code is as in netbeans example:
/**
*
* @author Alexander
*/
public class JavaFXSwingApplication1 extends JApplet {
private static final int JFXPANEL_WIDTH_INT = 300;
private static final int JFXPANEL_HEIGHT_INT = 250;
private static JFXPanel fxContainer;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
JFrame frame = new JFrame("JavaFX 2 in Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new JavaFXSwingApplication1();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
private void createScene() {
Parent parent = null;
try {
parent = FXMLLoader.load(getClass().getResource("LoginDialog.fxml"));
} catch (Throwable ex) {
ex.printStackTrace();
}
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
fxContainer.setScene(new Scene(parent));
}
}
I wonder if someone faced problem like this. Please help. I'm out of options.
I suspect the issue is somehow related with Java classloader.