Using a preconstructed Controller

The approach from the previous section will not work if you need to have certain data accessible at the time of the initialize() call. If the controller is set by the fx:controller attribute in FXML, it will be constructed automatically by JavaFX.

For such cases, there is an option to use a preconstructed controller:

public class PreconstructedController implements Initializable {        private final String newText;    public PreconstructedController(String newText) {        this.newText = newText;    }    @FXML    private Button button;    @FXML    private TextField textField;        @Override    public void initialize(URL url, ResourceBundle rb) {        button.setText(newText);    } }

Also, you need to remove the fx:controller attribute from the FXML ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.