Sunflame
Sunflame

Reputation: 3186

JavaFx: Scroll reset after TitledPane is collapsed

I am using TitledPanes ScrollPanes and TableViews and I have the problem, when I collapse a titledPane, the horizontal ScrollBar of the TableView resets.

Here is a code example where you can verify it:

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>

Any idea how can I prevent the scroll of the tableview to reset every time I collapse the pane?

Upvotes: 1

Views: 565

Answers (1)

kleopatra
kleopatra

Reputation: 51525

After a bit of digging, it looks like some layout optimization in VirtualFlow might be the reason (all seems to be fine if the scrolled content is not a TableView - not thoroughly analyzed, though)

What happens is:

  • during collapse, the TitledPane's content is resized vertically to 0
  • in VirtualFlow's layoutChildren a zero height/width is special cased to do nothing except hide everything, including the scrollBars
  • an internal listener to the scrollBar's visiblilty resets its value to 0

A tentative (read: dirty and might have unwanted side-effects, totally untested beyond this quick outline!) hack around is a custom TableViewSkin that tries to "remember" the last not-zero value and resets it on getting visible again.

An example:

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

}

Upvotes: 2

Related Questions