JavaFX 在 TableView 中使用多个 类?
JavaFX using multiple classes in a TableView?
我有一个问题:我需要创建一个 TableView,在其中显示一年内因不同疾病导致的所有死亡人数。问题是,在每一行中,我需要指定存储在 Deaths Class 中的 Province 字段。当我尝试构建 TableView 的不同列时,我使用了 Deaths 的每个字段,但是对于 Province 字段,我需要在省内使用特定的 method/field,即 getName()/name.
@FXML
private TableView<DecessiAnnuali> decessiTableView;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiIdColumn;
@FXML
private TableColumn<DecessiAnnuali, String> decessiProvinciaColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiAnnoColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiIncidentiStradaliColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieTumoraliColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieCardiovascolariColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieContagioseColumn;
private final DecessiAnnualiService decessiAnnualiService = new DecessiAnnualiService();
@Override
public void initialize(URL location, ResourceBundle resources) {
decessiIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
decessiAnnoColumn.setCellValueFactory(new PropertyValueFactory<>("anno"));
decessiProvinciaColumn.setCellValueFactory(new PropertyValueFactory<>("provincia"));
decessiIncidentiStradaliColumn.setCellValueFactory(new PropertyValueFactory<>("incidentiStradali"));
decessiMalattieTumoraliColumn.setCellValueFactory(new PropertyValueFactory<>("malattieTumorali"));
decessiMalattieContagioseColumn.setCellValueFactory(new PropertyValueFactory<>("malattieContagiose"));
decessiMalattieCardiovascolariColumn.setCellValueFactory(new PropertyValueFactory<>("malattieCardiovascolari"));}
如您所见,有一列包含 "provincia"(英文省份)。当我显示 table 时,它显然会打印省的 toString() 字符串(包含在 DecessiAnnuali(英语中的 AnnualDeaths)中)。我只需要获取Province中包含的name字段,而不是整个toString。
我假设您有一个 Provincia
class,并且 DecessiAnnuali
returns 中的 getProvincia()
方法是 Provincia
的一个实例] class。
鉴于此,您首先需要修复列的类型:
@FXML
private TableColumn<DecessiAnnuali, Provincia> decessiProvinciaColumn;
那么除了 cellValueFactory
之外,您还可以为省份列定义 cellFactory
。 (cellValueFactory
决定 什么 在单元格中显示,cellFactory
决定 如何 显示它。)
decessiProvinciaColumn.setCellValueFactory(new PropertyValueFactory<>("provincia"));
decessiProvinciaColumn.setCellFactory(column -> new TableCell<DecessiAnnuali, Provincia>() {
@Override
protected void updateItem(Provincia provincia, boolean empty) {
super.updateItem(provincia, empty);
if (empty || provincia == null) {
setText("");
else {
setText(provincia.getName());
}
}
});
我有一个问题:我需要创建一个 TableView,在其中显示一年内因不同疾病导致的所有死亡人数。问题是,在每一行中,我需要指定存储在 Deaths Class 中的 Province 字段。当我尝试构建 TableView 的不同列时,我使用了 Deaths 的每个字段,但是对于 Province 字段,我需要在省内使用特定的 method/field,即 getName()/name.
@FXML
private TableView<DecessiAnnuali> decessiTableView;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiIdColumn;
@FXML
private TableColumn<DecessiAnnuali, String> decessiProvinciaColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiAnnoColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiIncidentiStradaliColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieTumoraliColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieCardiovascolariColumn;
@FXML
private TableColumn<DecessiAnnuali,Integer> decessiMalattieContagioseColumn;
private final DecessiAnnualiService decessiAnnualiService = new DecessiAnnualiService();
@Override
public void initialize(URL location, ResourceBundle resources) {
decessiIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
decessiAnnoColumn.setCellValueFactory(new PropertyValueFactory<>("anno"));
decessiProvinciaColumn.setCellValueFactory(new PropertyValueFactory<>("provincia"));
decessiIncidentiStradaliColumn.setCellValueFactory(new PropertyValueFactory<>("incidentiStradali"));
decessiMalattieTumoraliColumn.setCellValueFactory(new PropertyValueFactory<>("malattieTumorali"));
decessiMalattieContagioseColumn.setCellValueFactory(new PropertyValueFactory<>("malattieContagiose"));
decessiMalattieCardiovascolariColumn.setCellValueFactory(new PropertyValueFactory<>("malattieCardiovascolari"));}
如您所见,有一列包含 "provincia"(英文省份)。当我显示 table 时,它显然会打印省的 toString() 字符串(包含在 DecessiAnnuali(英语中的 AnnualDeaths)中)。我只需要获取Province中包含的name字段,而不是整个toString。
我假设您有一个 Provincia
class,并且 DecessiAnnuali
returns 中的 getProvincia()
方法是 Provincia
的一个实例] class。
鉴于此,您首先需要修复列的类型:
@FXML
private TableColumn<DecessiAnnuali, Provincia> decessiProvinciaColumn;
那么除了 cellValueFactory
之外,您还可以为省份列定义 cellFactory
。 (cellValueFactory
决定 什么 在单元格中显示,cellFactory
决定 如何 显示它。)
decessiProvinciaColumn.setCellValueFactory(new PropertyValueFactory<>("provincia"));
decessiProvinciaColumn.setCellFactory(column -> new TableCell<DecessiAnnuali, Provincia>() {
@Override
protected void updateItem(Provincia provincia, boolean empty) {
super.updateItem(provincia, empty);
if (empty || provincia == null) {
setText("");
else {
setText(provincia.getName());
}
}
});