簡體   English   中英

向下滾動並雙擊最后一個展開/折疊箭頭時,TreeTableView 項目消失

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

在使用TreeTableView我意識到當您向下滾動表格並雙擊最后一個展開/折疊箭頭時,所有項目都會消失。 但是,當您再次滾動時,所有項目都會重新出現。 當然,當您有足夠的項目以便垂直ScrollBar處於活動狀態時,就會發生這種情況。

有沒有人遇到過這個問題? 這是一個已知的問題?

簡單的例子:

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

}

我已經測試了這段代碼:

  • 硬件/操作系統: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

自 JavaFX 16 以來,此問題已解決。 但是,這里有一個適用於舊 JavaFX 版本的hacky解決方案。

它通過向TreeTableView expandedItemCountProperty()添加一個偵聽器來強制TreeTableView在需要時刷新:

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

注意:使用問題中的示例進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM