fxml 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: refrence xml textfield in javafx

fx:controller=<Controller.Location>

@FXML TextField textfield;

fx:id=<idFromController>

textfield.getText();
textfield.setText();

Example 3: 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