Firstly, my Maven file, as I'm not sure if I am using the newest version of JavaFX or not:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
My main method (just a proof of concept:
@Override
public void start(Stage primaryStage) {
Clinica c = new Clinica();
c.setNombre("Nombre");
c.setCodigoPostal("CP");
c.setPoblacion("Malaga");
c.setDireccion("Calle Se");
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Sample.fxml"));
SampleController sc=new SampleController();
sc.setClinic(c);
loader.setController(sc);
Parent root = loader.load();
Scene scene = new Scene(root, 800, 600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
My controller:
public class SampleController implements Initializable {
@FXML
private TextField name;
@FXML
private TextField zip;
@FXML
private TextField town;
@FXML
private TextField address;
private Clinica c;
public SampleController() {
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.name = new TextField(this.c.getNombre());
this.zip = new TextField(this.c.getCodigoPostal());
this.town = new TextField(this.c.getPoblacion());
this.address = new TextField(this.c.getDireccion());
}
public void setClinic(Clinica c) {
this.c = c;
}
}
And my FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Do not edit this file it is generated by e(fx)clipse from ../src/main/resources/es/ortoplus/migration/Sample.fxgraph
-->
<?import java.lang.*?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.GridPane?>
<GridPane xmlns:fx="http://javafx.com/fxml" vgap="5" hgap="5" alignment="CENTER">
<children>
<TextField id="name" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
<TextField id="zip" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
<TextField id="town" GridPane.rowIndex="3" GridPane.columnIndex="1"/>
<TextField id="address" GridPane.rowIndex="4" GridPane.columnIndex="1"/>
</children>
</GridPane>
The problem: I cannot populate the form.
I've tried not creating instances in the initialize method, expecting that the binding and injection were automatically done, but all controls were null when the initialize method is executed (so I had a NPE).
Then, as the code shows, I initialized the fields manually and set the values, but the form is shown empty and it seems there is no binding between view and the controller.
What am I doing wrong?