5

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
fabian
  • 80,457
  • 12
  • 86
  • 114
  • What does "refuses to run" mean? Do you get any error messages? – James_D Mar 29 '16 at 13:44
  • 1
    This line `fx:include source="Tab1.fxml"/>` is missing an angle bracket at the start: `<` – ManoDestra Mar 29 '16 at 13:54
  • Missing bracket was a typo. I have inserted the error i get when i try to run it.Thanks –  Mar 29 '16 at 14:08
  • `Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97` Coming from this location. Can you post your controller's code as well? – ManoDestra Mar 29 '16 at 14:26
  • Thank you i have uploaded the controller code –  Mar 29 '16 at 21:48
  • Wondering why the FXML file was deleted from this question at some point. The fact that there is a `` element is pretty important to the question. – James_D Jan 10 '18 at 17:49

1 Answers1

5

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();
James_D
  • 201,275
  • 16
  • 291
  • 322
  • I was able to get it to work using an `InputStream` by using the absolute path of the included fxml file in the fx:include tag. However, I think changing to use `setLocation` is the better way. – Danny Harding Sep 12 '17 at 20:41