简体   繁体   中英

TreeTableView items disappear when scroll down and double-click last expand/collapse arrow

While working with a TreeTableView I realized that when you scroll down the table and double click the last expand/collapse arrow, all items disappear. However, when you scroll again all items reappear. Of course, this happens when you have enough items so the vertical ScrollBar is active.

Does anyone experience this issue before? Is this a known issue?

Simple example:

import java.util.Arrays;
import java.util.List;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.StackPane;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;

public class App extends Application {

    @Override
    public void start(Stage stage) {

        TreeTableView<List<String>> table = new TreeTableView<>();
        table.setMaxHeight(250);

        TreeTableColumn<List<String>, String> colCity = new TreeTableColumn<>("City");
        TreeTableColumn<List<String>, String> colCountry = new TreeTableColumn<>("Country");

        colCity.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getValue().get(0)));
        colCountry.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getValue().get(1)));

        table.getColumns().setAll(colCity, colCountry);

        TreeItem<List<String>> root = new TreeItem<>(Arrays.asList("Root", ""));
        root.setExpanded(true);

        for (int i = 0; i < 10; i++) {
            TreeItem<List<String>> item = new TreeItem<>(List.of("London", "UK"));
            item.getChildren().add(new TreeItem<>(List.of("New York", "US")));
            item.setExpanded(true);
            root.getChildren().add(item);
        }

        table.setRoot(root);

        Scene scene = new Scene(new StackPane(table));

        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

I have tested this code on:

  • Hardware/OS: MacBookPro16,1/macOS Catalina 10.15.6
  • Java: 11.0.2, 14.0.1
  • JavaFX: 11.0.2, 12.0.2, 13.0.2, 14.0.2.1, 15

Since JavaFX 16 this issue is solved. However, here's a hacky solution for older JavaFX versions.

It works by adding a listener to the expandedItemCountProperty() to force the TreeTableView to refresh when needed:

table.expandedItemCountProperty().addListener(e -> {

    VirtualFlow<?> virtualFlow = (VirtualFlow<?>) table.lookup(".virtual-flow");

    if (virtualFlow != null && virtualFlow.getFirstVisibleCell() != null) {

        int firstVisibleIndex = virtualFlow.getFirstVisibleCell().getIndex();
        int visibleCells = virtualFlow.getLastVisibleCell().getIndex() - firstVisibleIndex;

            if (firstVisibleIndex > visibleCells) {
                table.refresh();
            }
    }
});

Note: tested using the example in the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM