0

I was trying to load a fxml file and then change the properties of its objects (like the text of a button). I know how to load the fxml but I can't seem to figure out how to change the properties (like the text of a button) of objects

I know that you can somehow interact with objects by calling their Id but I dont know how to do that. So how do you interact with objects in a fxml file.

Code:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;

public class App extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(new URL("file:///PathToFXML"));
        VBox vbox = loader.<VBox>load();

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>

<VBox xmlns:fx="http://javafx.com/fxml">
    <Button fx:id="button" text="Button"/>
</VBox>

0 Answers0