TreeTableView向下滚动并双击最后一个展开/折叠箭头时,项目会消失。

huangapple go评论68阅读模式
英文:

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&lt;List&lt;String&gt;&gt; table = new TreeTableView&lt;&gt;();
table.setMaxHeight(250);
TreeTableColumn&lt;List&lt;String&gt;, String&gt; colCity = new TreeTableColumn&lt;&gt;(&quot;City&quot;);
TreeTableColumn&lt;List&lt;String&gt;, String&gt; colCountry = new TreeTableColumn&lt;&gt;(&quot;Country&quot;);
colCity.setCellValueFactory(c -&gt; new SimpleStringProperty(c.getValue().getValue().get(0)));
colCountry.setCellValueFactory(c -&gt; new SimpleStringProperty(c.getValue().getValue().get(1)));
table.getColumns().setAll(colCity, colCountry);
TreeItem&lt;List&lt;String&gt;&gt; root = new TreeItem&lt;&gt;(Arrays.asList(&quot;Root&quot;, &quot;&quot;));
root.setExpanded(true);
for (int i = 0; i &lt; 10; i++) {
TreeItem&lt;List&lt;String&gt;&gt; item = new TreeItem&lt;&gt;(List.of(&quot;London&quot;, &quot;UK&quot;));
item.getChildren().add(new TreeItem&lt;&gt;(List.of(&quot;New York&quot;, &quot;US&quot;)));
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 -&gt; {
VirtualFlow&lt;?&gt; virtualFlow = (VirtualFlow&lt;?&gt;) table.lookup(&quot;.virtual-flow&quot;);
if (virtualFlow != null &amp;&amp; virtualFlow.getFirstVisibleCell() != null) {
int firstVisibleIndex = virtualFlow.getFirstVisibleCell().getIndex();
int visibleCells = virtualFlow.getLastVisibleCell().getIndex() - firstVisibleIndex;
if (firstVisibleIndex &gt; visibleCells) {
table.refresh();
}
}
});

Note: tested using the example in the question.

huangapple
  • 本文由 发表于 2020年9月11日 04:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63836998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定