Search code examples
javafxjavafx-8treetableview

JavaFx TreeTableView Real Time Data


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

Solution

  • Resolved this issue by using ListProperty