I have a custom class inside a fxml file called TablePatientView. I use it like <TablePatientView fx:id="tablePatientView" layoutX="20.0" layoutY="45.0" prefHeight="409.0" prefWidth="363.0" /> and it works almost fine. The fx:root looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<fx:root type="javafx.scene.control.TableView" xmlns:fx="http://javafx.com/fxml">
<TableView>
<columns>
<TableColumn fx:id="patId" prefWidth="101.0" text="Patient-ID" />
<TableColumn fx:id="patVorname" prefWidth="69.0" text="Vorname" />
<TableColumn fx:id="patNachname" prefWidth="98.0" text="Nachname" />
<TableColumn fx:id="patGebdat" prefWidth="96.0" text="Geburtsdatum" />
</columns>
</TableView>
</fx:root>
with the following class
public class TablePatientView extends TableView<TablePatient> {
private Parent root;
@FXML
private TableColumn<TablePatient,String> patId;
@FXML
private TableColumn<TablePatient,String> patVorname;
@FXML
private TableColumn<TablePatient,String> patNachname;
@FXML
private TableColumn<TablePatient, LocalDate> patGebdat;
public TablePatientView() {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/fxml/TablePatientView.fxml"));
loader.setController(this);
loader.setRoot(this);
try {
root = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
setCellValueFactories();
// getColumns().addAll(patId, patVorname, patNachname, patGebdat); works, but not with plain fxml
}
private void setCellValueFactories() {
patId.setCellValueFactory(param -> param.getValue().idProperty().asString());
patVorname.setCellValueFactory(param -> param.getValue().vornameProperty());
patNachname.setCellValueFactory(param -> param.getValue().nachnameProperty());
patGebdat.setCellValueFactory(param -> param.getValue().gebDatProperty());
}
}
The columns are correctly initialized, but for some reason not added as columns to the TableView. I know I can do it via java code (see the commented line in the code above) but I would like to know why this doesn't work with just fxml. Using the <TableView> directly inside fxml without a custom class and root works just fine.