JavaFx:折叠 TitledPane 后滚动重置

JavaFx: Scroll reset after TitledPane is collapsed

我正在使用 TitledPanes ScrollPanes 和 TableViews 并且我遇到了问题,当我折叠一个 titledPane 时,[= 的水平 ScrollBar 14=] 重置。

这是一个代码示例,您可以在其中验证它:

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableView;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private AnchorPane content;
    @FXML
    private TitledPane titledPane;
    @FXML
    private TableView<Object> tableView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        titledPane.prefHeightProperty().bind(content.heightProperty());
        tableView.prefWidthProperty().bind(content.widthProperty());

        tableView.getColumns().forEach(col -> col.setPrefWidth(300)); // to have enough "space" to scroll

        tableView.setItems(FXCollections.observableArrayList(new Object()));
    }

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="Whosebug.testscroll.Controller"
            fx:id="content">
            <TitledPane fx:id="titledPane">
                <TableView fx:id="tableView">
                    <columns>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                    </columns>
                </TableView>
           </TitledPane>
</AnchorPane>

知道如何防止每次折叠窗格时重置 tableview 的滚动吗?

经过一些挖掘,看起来 VirtualFlow 中的一些布局优化可能是原因(如果滚动的内容不是 TableView,一切似乎都很好 - 虽然没有彻底分析)

发生的情况是:

  • 在折叠期间,TitledPane 的内容垂直调整为 0
  • 在 VirtualFlow 的 layoutChildren 中,零 height/width 是特殊情况,除了隐藏所有内容外什么都不做,包括 scrollBars
  • 滚动条可见性的内部侦听器将其值重置为 0

一个试探性的(阅读:脏的并且可能有不需要的副作用,除了这个快速概述之外完全未经测试!)hack around 是一个自定义 TableViewSkin,它试图 "remember" 最后一个非零值并重置它再次可见。

一个例子:

public class TitledPaneTableScroll extends Application {

    public static class TableViewScrollSkin<T> extends TableViewSkin<T> {

        DoubleProperty hvalue = new SimpleDoubleProperty();

         public TableViewScrollSkin(TableView<T> control) {
            super(control);
            installHBarTweak();
        }

        private void installHBarTweak() {
            // Note: flow and bar could be legally retrieved via lookup 
            // protected api pre-fx9 and post-fx9
            VirtualFlow<?> flow = getVirtualFlow();
            // access scrollBar via reflection 
            // this is my personal reflective access utility method - use your own :)
            ScrollBar bar = (ScrollBar) FXUtils
                    .invokeGetFieldValue(VirtualFlow.class, flow, "hbar");
            bar.valueProperty().addListener((s, o, n) -> {
                if (n.intValue() != 0) {
                    hvalue.set(n.doubleValue());
                    // debugging
                    //  new RuntimeException("who is calling? \n").printStackTrace();
                } 
                //LOG.info("hbar value: " + n + "visible? " + bar.isVisible());
            });

            bar.visibleProperty().addListener((s, o, n) -> {
                if (n) {
                    bar.setValue(hvalue.get());
                } 
            });
        }
    }

    int counter;
    private Parent createContent() {

        TableView<Object> table = new TableView<>(FXCollections.observableArrayList(new Object()) ) {

            @Override
            protected Skin<?> createDefaultSkin() {
                return new TableViewScrollSkin<>(this);
            }

        };
        table.getColumns().addAll(Stream
                .generate(TableColumn::new)
                .limit(10)
                .map(col -> {
                    col.setPrefWidth(50);
                    col.setText("" + counter++);
                    return col;
                })
                .collect(Collectors.toList())); 


        TitledPane titled = new TitledPane("title", table);
        titled.setAnimated(true);

        BorderPane content = new BorderPane(titled);
        return content;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent(), 400, 400));
       // stage.setTitle(FXUtils.version());
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(TitledPaneTableScroll.class.getName());

}