Java Reference
In-Depth Information
import java.util.ResourceBundle;
public class FXMLInjectionExampleMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(
FXMLInjectionExampleMain.class.getResource("/FXMLInjectionExample.fxml"));
fxmlLoader.setResources(ResourceBundle.getBundle("FXMLInjectionExample"));
VBox vBox = fxmlLoader.load();
Scene scene = new Scene(vBox);
primaryStage.setTitle("FXML Injection Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Notice that we also created an empty FXMLInjectionExample.properties file to use as the resource bundle to
illustrate the injection of the resources field into the controller. We explain how to use resource bundles with FXML
files in the next section. When the FXMLInjectionExample is run on my machine, the FXML Injection Example
window in Figure 3-5 is displayed on the screen.
Figure 3-5. The FXMLInjection program
The @FXML annotation can also be used for included FXML file controller injection, and for marking controller
properties of javafx.event.EventHandler type for use as event handlers in FXML files. We cover them in detail when
we discuss the relevant features of the FXML file in the next section.
Exploring the Capabilities of FXML Files
In this section, we go over the features of the FXML file format. Because a major goal of the FXMLLoader is to
deserialize an FXML file into Java objects, it should be no surprise that it provides facilities that help to make writing
FXML files easier.
 
Search WWH ::




Custom Search