FXML Doccument refusing to import other fxml files

梦想与她 提交于 2020-01-04 04:23:32

问题


I have a main FXML document for my program which contains a TabPane. For each tab I want it to have its own controller and fxml file. When I try to include the external fmxl files into the main fxml document, my program refuses to run. here is my main FXML document: here is a copy of my java file

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader fxml = new FXMLLoader();
    Parent root = fxml.load(getClass().getResource("FXMLDocument.fxml").openStream());

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    FXMLDocumentController fdc = fxml.getController();
}

Error:

Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97

回答1:


This error is caused because you have not set the location property of the FXMLLoader, and instead you are specifying an InputStream from which to load the FXML. I think the FXMLLoader must need to know the location of the original fxml file in order to resolve the location of the included file. You should really only use the load(InputStream) method in exceptional circumstances: when you are loading the fxml from a source other than a resource (i.e. file or resource in your application jar file).

Instead, use

FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();

or, equivalently,

FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();


来源:https://stackoverflow.com/questions/36285996/fxml-doccument-refusing-to-import-other-fxml-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!