问题 - 无法在 TreeTableView 中填充实时数据。数据来自外部来源,需要在 TreeTableView 中实时显示
下面是代码的简洁部分。能够使用节点创建树表视图。为了测试,我创建了一个随机数生成器并将其与表树视图字段绑定。不确定这是否是正确的方法。
希望我已经设法清楚地解释了这个问题。期待指导。
FXML:
<TreeTableColumn fx:id="car" prefWidth="150.0" text="Car" />
<TreeTableColumn fx:id="latest" prefWidth="75.0" text="Latest" />
<TreeTableColumn fx:id="high" prefWidth="75.0" text="High" />
<TreeTableColumn fx:id="low" prefWidth="75.0" text="Low" />
控制器:
public void initialize(URL location, ResourceBundle resources) {
// Initialize Table Views
car.setCellValueFactory((CellDataFeatures<TableCarMain, String> p) -> new ReadOnlyStringWrapper(
p.getValue().getValue().getCar()));
latest.setCellValueFactory((CellDataFeatures<TableCarMain, String> p) -> new ReadOnlyStringWrapper(
p.getValue().getValue().getLatest()));
high.setCellValueFactory((CellDataFeatures<TableCarMain, String> p) -> new ReadOnlyStringWrapper(
p.getValue().getValue().getHigh()));
low.setCellValueFactory((CellDataFeatures<TableCarMain, String> p) -> new ReadOnlyStringWrapper(
p.getValue().getValue().getLow()));
// Create Main Nodes
final TreeItem<TableCarMain> root = new TreeItem<>(new TableCarMain("Vehicles"));
final TreeItem<TableCarMain> nodeAuto = new TreeItem<>(new TableCarMain("AutomaticCar"));
final TreeItem<TableCarMain> nodeManual = new TreeItem<>(new TableCarMain("ManualCar"));
//Child Nodes - Getting data from Excel & creating the child nodes
for (List<XSSFCell> line : filteredData) {
if (line.get(3).getStringCellValue().equals("AutomaticCar")) {
nodeAuto.getChildren().add(new TreeItem<>(new TableCarMain(line.get(0).getStringCellValue())));
}
if (line.get(3).getStringCellValue().equals("ManualCar")) {
nodeManual.getChildren().add(new TreeItem<>(new TableCarMain(line.get(0).getStringCellValue())));
}
}
root.setExpanded(true);
root.getChildren().addAll(nodeAuto , nodeManual);
tvTree.setRoot(root);
出于测试目的,我在 FXML 中添加了一个按钮,将以下代码链接到它并添加了一个随机数生成器来模拟我期望获得的实时数据。这个想法是在我按下开始按钮后,随着随机数生成值,最新列中的值应该显示该值。现在我没有将任何值传递给高和低列,因为它不适用于最新
@FXML
FloatProperty latestProperty= new SimpleFloatProperty();
FloatProperty externalDataProperty= new SimpleFloatProperty();
private void getData() {
latestProperty.bind(externalDataProperty);
ObservableList<TableTradeMain> lineData = FXCollections.observableArrayList();
for (List<XSSFCell> line : filteredData) {
lineData.add(new TableCarMain(line.get(0).getStringCellValue(), Float.toString(latestProperty.get())));
}
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
externalDataProperty.set(randomGenerator.nextFloat());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}