JavaFx:TitledPane折叠后滚动重置

问题描述 投票:1回答:1

我正在使用TitledPanes ScrollPanes和TableViews我有问题,当我崩溃titledPane时,ScrollBar的水平TableView重置。

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

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="stackoverflow.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的滚动重置?

java javafx scroll tableview javafx-8
1个回答
2
投票

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

会发生什么:

  • 在崩溃期间,TitledPane的内容垂直调整为0
  • 在VirtualFlow的layoutChildren中,零高度/宽度是特殊的,除了隐藏所有内容之外什么都不做,包括滚动条
  • scrollBar的visiblilty的内部侦听器将其值重置为0

一个暂定的(读取:脏,可能有不必要的副作用,完全未经测试超出这个快速轮廓!)黑客是一个自定义TableViewSkin,试图“记住”最后一个非零值并重置它再次可见。

一个例子:

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());

}
© www.soinside.com 2019 - 2024. All rights reserved.