【发布时间】:2022-01-19 15:03:00
【问题描述】:
我在名为TablePatientView 的 fxml 文件中有一个自定义类。我像<TablePatientView fx:id="tablePatientView" layoutX="20.0" layoutY="45.0" prefHeight="409.0" prefWidth="363.0" /> 一样使用它,它几乎可以正常工作。 fx:root 如下所示:
<?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>
下面的类
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());
}
}
列已正确初始化,但由于某种原因未作为列添加到 TableView。
我知道我可以通过 java 代码来做到这一点(参见上面代码中的注释行),但我想知道为什么这不适用于 fxml。直接在 fxml 中使用
【问题讨论】: