fxml in javafx code example

Example 1: javafx application fxml

FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Path/To/file.fxml"));
root = loader.load();

Scene scene = new Scene(root, width, height);
primaryStage.setScene(scene);

primaryStage.setTitle("Title");
primaryStage.show();

Example 2: javafx access fxml elements

Scene scene = stage.getScene();
//capture elements by their #id: 
Button btn = (Button) scene.lookup("#myBtnID");
TextField txt = (TextField ) scene.lookup("#myTxtID");

Tags:

Java Example