// 值1
List<String> data1 = FXCollections.observableArrayList("时间", "pH");
// 值2
List<String> data2 = FXCollections.observableArrayList("最大值", "最小值");
// 值3
List<List<String>> data3 = FXCollections.observableArrayList();
List<String> item1 = FXCollections.observableArrayList("值1", "值2");
List<String> item2 = FXCollections.observableArrayList("值3", "值4");
List<String> item3 = FXCollections.observableArrayList("值5", "值6");
data3.add(item1);
data3.add(item2);
data3.add(item3);
for (int j = 0; j < data1.size(); j++) {
TableColumn<List<String>, String> column1 = new TableColumn<>(data1.get(j));
if (j != 0) {
for (int k = 0; k < data2.size(); k++) {
List<TableColumn<List<String>, String>> secondColumn = new ArrayList<>();
TableColumn<List<String>, String> column2 = new TableColumn<>(data2.get(k));
column2.setCellValueFactory(new PropertyValueFactory<>("column"));
column2.setPrefWidth(150);
for (int i = 0; i < data3.size(); i++) {
final int columnIndex = k;
column2.setCellValueFactory(cellData -> {
return new SimpleStringProperty(cellData.getValue().get(columnIndex));
});
}
secondColumn.add(column2);
column1.getColumns().addAll(secondColumn);
}
} else {
column1.setCellValueFactory(cellData -> {
return new SimpleStringProperty(cellData.getValue().get(0));
});
column1.setPrefWidth(150);
}
treeTableView.getColumns().addAll(column1);
}
// 设置表格的列宽自适应:
// columnResizePolicy:决定列的调整策略,CONSTRAINED_RESIZE_POLICY (约束调整,根据内容自动调整列宽)
// UNCONSTRAINED_RESIZE_POLICY (无约束调整,用户可以自由调整列宽
Integer titlenum = (data1.size() - 1) * data2.size();
if (titlenum <= 6) {
treeTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
} else {
treeTableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
}
treeTableView.setItems(FXCollections.observableList(data3));
06-01
06-01
08-17
1176
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
2994
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)