英文:
TreeTableView items disappear when scroll down and double-click last expand/collapse arrow
问题
在使用TreeTableView
时,我注意到当你向下滚动表格并双击最后一个展开/折叠箭头时,所有的项目都会消失。然而,当你再次滚动时,所有项目都会重新出现。当然,这发生在你有足够的项目以使垂直滚动条生效的情况下。
有人之前遇到过这个问题吗?这是一个已知的问题吗?
简单示例:
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
英文:
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
答案1
得分: 0
自从JavaFX 16版本以后,这个问题就得到了解决。然而,这里有一个对于旧版本JavaFX的巧妙解决方案。
它通过向 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();
}
}
});
注意:使用问题中的示例进行了测试。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论