[英]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();
}
}
我已經測試了這段代碼:
自 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.