Always visible when scrolled anchored Node in Java FX 2.0?

被刻印的时光 ゝ 提交于 2019-12-01 12:31:25

问题


I want to make a node present in a ScrollPane anchored to the top (fixed on the Y axis) but capable of scrolling in the X axis. (In Java-FX 2.0)

Is this possible?


回答1:


You can use binding to adjust this special object position based on scrollbar position, see next code:

public class FancyScrollPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        ScrollPane scrollPane = new ScrollPane();
        Pane content = new Pane();
        scrollPane.setContent(content);

        // adding background
        content.getChildren().add(new Rectangle(500, 500, Color.GREEN));

        Circle immovableObject = new Circle(30, Color.RED);
        content.getChildren().add(immovableObject);

        primaryStage.setScene(new Scene(scrollPane, 300, 300));
        primaryStage.show();

        // here we bind circle Y position
        immovableObject.layoutYProperty().bind(
                // to vertical scroll shift (which ranges from 0 to 1)
                scrollPane.vvalueProperty()
                    // multiplied by (scrollableAreaHeight - visibleViewportHeight)
                    .multiply(
                        content.heightProperty()
                            .subtract(
                                new ScrollPaneViewPortHeightBinding(scrollPane))));
    }

    // we need this class because Bounds object doesn't support binding 
    private static class ScrollPaneViewPortHeightBinding extends DoubleBinding {

        private final ScrollPane root;

        public ScrollPaneViewPortHeightBinding(ScrollPane root) {
            this.root = root;
            super.bind(root.viewportBoundsProperty());
        }

        @Override
        protected double computeValue() {
            return root.getViewportBounds().getHeight();
        }
    }

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


来源:https://stackoverflow.com/questions/10238834/always-visible-when-scrolled-anchored-node-in-java-fx-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!