【发布时间】:2016-04-06 10:49:36
【问题描述】:
问题 - 无法在 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();
}
}
}
【问题讨论】:
-
您在 FX 应用程序线程上编写代码块 (
sleep()s),因此在循环完成之前您不会看到任何 UI 更新。换句话说,它将导致您的 UI 冻结 10 秒,然后显示循环中生成的最后一个值。您需要将该代码移至后台线程。 -
@James_D 我已经注释掉了睡眠部分,因为它不是必需的,但标题为 latest 的列中仍然没有输出。我想我在填充 TreeTableView 的方式上犯了一些错误,无法弄清楚
-
嗯...你在更新树中任何东西的值吗?看起来您每次都在更新
externalDataProperty,这将更新latestProperty,但不会更新任何TableCarMain实例中的任何属性。您需要更新树表中显示的模型对象的属性。 -
@James_D 是的,我需要用值填充树。树中的第一列将第一次填充,而另一列中的值需要填充流动的实时数据。目前正在测试,因此只检查该方法是否能够在数据发生变化时填充 1 列。希望很清楚。
-
我认为你错过了我的观点。如果您的代码不更新树表中显示的值,您将不会看到任何事情发生。
标签: javafx javafx-8 treetableview