0
votes

Issue - Unable to populate realtime data in TreeTableView. The data is coming from a external source & needs to be shown in the TreeTableView in realtime

Below is a concise part of the code. Am able to create the Tree Table View with the nodes. For testing, I have created a random number generator & bound that with the table tree view field. Not sure if this is the correct approach.

Hope I have managed to explain the issue clearly. Look forward to guidance.

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" />

Controller:

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);

For testing purpose I have added a Button in FXML, linked the below code to it & added a random number generator to simulate the realtime data which I expect to get. The idea is after I press the start button, as the Random Number generates values, the value in the Latest column should show the value. For now I am not passing any value to columns High & Low as it is not working for Latest

@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();
        }
        }
}
1
You code blocks (sleep()s) on the FX Application Thread, so you won't see any UI updates until the loop is finished. In other words, it will cause your UI to freeze for 10 seconds and then show the last value generated in the loop. You need to move that code to a background thread. - James_D
@James_D I have commented out the sleep portion as it is not required, but still no output in the column titled latest. I guess I am making some mistake in the way I am populating the TreeTableView unable to figure it out - iCoder
Well... are you updating the values of anything in the tree? It just looks like you are updating externalDataProperty each time, which will update latestProperty, but will not update any properties in any TableCarMain instances. You need to update properties in the model objects displayed in the treetable. - James_D
@James_D yes I need to populate the tree with values. The first column in the tree will be populated the first time & the values in the other column need to be populated with the real time data which flows. At present am testing & hence only checking to see if the approach is able to populate 1 column with the data as it changes. Hope am clear. - iCoder
I think you missed my point. You're not going to see anything happen if your code doesn't update the values that are being displayed in the tree table. - James_D

1 Answers

0
votes

Resolved this issue by using ListProperty