0

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?

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • 1
    replace `id` by `fx:id` in your FXML file and do not instantiate `TextField` again in your `ìnitialize` method. – Pagbo Jan 08 '19 at 17:09
  • @Pagbo Thank you! I've been blocked by the whole day for that silly mistake – Pablo Lozano Jan 08 '19 at 17:16
  • 2
    You're welcome. Just to be more accurate (if s.o read this), `id` is used for CSS (styling component) and `fx:id` for injection. Beside note, you can remove `initialize` arguments and the interface implementation and add the `@FXML` annotation instead : something like `@FXML private void initialize(){ ... }`. This is cleaner. – Pagbo Jan 08 '19 at 17:24

0 Answers0