JavaFX 8加载外部FXML

时间:2015-06-08 08:26:52

标签: javafx javafx-8 fxml fxmlloader

在查看JavaFX SceneBuilder时,它可以加载任何类型的fxml。根节点或控制器被忽略。 scenebuilder能够加载任何类型的fxml并可以显示其组件。

有没有人喜欢加载fxml指导其字段的id,控制器或根节点类型?

问题是,当我尝试加载任何fxml时,如果我设置了正确的根类型,它只能加载。我想加载(通过FileChooser)我可以找到的任何FXML(比如说)窗格。

任何想法都会受到赞赏(www中的所有帮助都指向使用特定根类型的加载机制,因此根类型是已知的)。我想加载未知的根类型而忽略fx:id等。

例外:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$2/1531448569.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: 
/C:/Users/Dominik/Documents/fhnw/bath/svn/examples/fxmlloader/out/production/FxHeatMap/eu/hansolo/fx/heatmap/fxmlload/Application.fxml:20

    at eu.hansolo.fx.heatmap.fxmlload.FXMLLoader.start(FXMLLoader.java:34)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$66/84373524.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1537074899.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/1947223685.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/835743810.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/1962582134.run(Unknown Source)
    ... 1 more
Caused by: javafx.fxml.LoadException: 
/C:/Users/Dominik/Documents/fhnw/bath/svn/examples/fxmlloader/out/production/FxHeatMap/eu/hansolo/fx/heatmap/fxmlload/Application.fxml:20

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:918)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at eu.hansolo.fx.heatmap.fxmlload.FXMLLoader.start(FXMLLoader.java:32)
    ... 14 more
Caused by: java.lang.ClassNotFoundException: sample.MainController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:916)
    ... 27 more

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

fx:controllerfx:root个属性。不要困惑。您可以通过将加载的对象强制转换为Parent超类来忽略根布局类型。但我认为没有办法忽视fxml文件中定义的fx:controller。在这种情况下,您需要的是自己加载文件并从文件中删除fx:controller及其值。