问题 - 无法在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中添加了一个Button,将以下代码链接到它&amp;添加了一个随机数生成器来模拟我期望得到的实时数据。我的想法是在按下开始按钮后,随机数生成值,最新列中的值应显示该值。现在我没有将任何值传递给High&amp;低,因为它不适用于最新
@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();
}
}
}
答案 0 :(得分:0)
使用ListProperty
解决了此问题