向下滚动并双击最后一个 expand/collapse 箭头时,TreeTableView 项目消失
TreeTableView items disappear when scroll down and double-click last expand/collapse arrow
在使用 TreeTableView
时,我意识到当您向下滚动 table 并双击最后一个 expand/collapse 箭头时,所有项目都会消失。但是,当您再次滚动时,所有项目都会重新出现。当然,当您有足够的项目时会发生这种情况,因此垂直 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();
}
}
我已经在以下平台上测试了这段代码:
- Hardware/OS: MacBookPro16,1/macOS Catalina 10.15.6
- Java: 11.0.2, 14.0.1
- Java外汇:11.0.2、12.0.2、13.0.2、14.0.2.1、15
从 JavaFX 16 开始,这个问题就解决了。然而,这里有一个针对旧 JavaFX 版本的 hacky 解决方案。
它的工作原理是向 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();
}
}
});
注意:使用问题中的示例进行测试。
在使用 TreeTableView
时,我意识到当您向下滚动 table 并双击最后一个 expand/collapse 箭头时,所有项目都会消失。但是,当您再次滚动时,所有项目都会重新出现。当然,当您有足够的项目时会发生这种情况,因此垂直 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();
}
}
我已经在以下平台上测试了这段代码:
- Hardware/OS: MacBookPro16,1/macOS Catalina 10.15.6
- Java: 11.0.2, 14.0.1
- Java外汇:11.0.2、12.0.2、13.0.2、14.0.2.1、15
从 JavaFX 16 开始,这个问题就解决了。然而,这里有一个针对旧 JavaFX 版本的 hacky 解决方案。
它的工作原理是向 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();
}
}
});
注意:使用问题中的示例进行测试。