For some reason, my fx:id does not bind properly to my Controller
class, and thus always causes an error.
Controller
package sample;
import javafx.fxml.FXML;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Controller {
@FXML public Button button;
public void clickAction(ActionEvent actionEvent) {
System.out.println("Button clicked.");
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<Button text="Click me!" fx:id="button" onAction="#clickAction" BorderPane.alignment="CENTER"/>
</BorderPane>
I think I understand the source of my problem, but I do not understand how to properly address it. According an answer of this question, I think I am trying to assign FXML elements before the constructor is called (and these elements can only be assigned during/after initialisation).
Is there a way to do this without implementing Initializable
? Or am I making a completely different mistake?