Java Reference
In-Depth Information
private StageStyle configStageStyle() {
StageStyle stageStyle = StageStyle.DECORATED;
List<String> unnamedParams = getParameters().getUnnamed();
if (unnamedParams.size() > 0) {
String stageStyleParam = unnamedParams.get(0);
if (stageStyleParam.equalsIgnoreCase("transparent")) {
stageStyle = StageStyle.TRANSPARENT;
} else if (stageStyleParam.equalsIgnoreCase("undecorated")) {
stageStyle = StageStyle.UNDECORATED;
} else if (stageStyleParam.equalsIgnoreCase("utility")) {
stageStyle = StageStyle.UTILITY;
}
}
return stageStyle;
}
}
Before looking at the FXMLLoader code, let me point out that for this example, we choose to put the StageCoach.fxml
file together with the StageCoachMain.java and the StageCoachController.java files. And they all reside in the
projavafx/stagecoach/ui directory. That relation is preserved when we compile the source files. Therefore when we
run this program, the FXML file appears as a resource /projavafx/stagecoach/ui/StageCoach.fxml in the classpath.
Figure 3-2 illustrates the file layout of our example.
Figure 3-2. The file layout of the StageCoach example
The loading of the FXML file is performed by the following snippet of code:
FXMLLoader fxmlLoader = new FXMLLoader(StageCoachMain.class
.getResource("/projavafx/stagecoach/ui/StageCoach.fxml"));
Group rootGroup = fxmlLoader.load();
final StageCoachController controller = fxmlLoader.getController();
Here we use the one-parameter constructor of the FXMLLoader class to construct an fxmlLoader object and
pass in a URL object returned by the getResource() call on the Class object of StageCoachMain . This URL object is
a jar URL or a file URL, depending on whether you run this program from a jar. We then call the load() method on
the fxmlLoader object. This method reads the FXML file, parses it, instantiates all the nodes it specified, and hooks
them up according to the containment relationships it specified. Because a controller is specified in the FXML file,
the method also instantiates a StageCoachController instance and assigns the nodes to the fields of the controller
instance according to the fx:id s. This step is usually called injecting the FXML nodes into the controller. The event
Search WWH ::




Custom Search