Want to trigger scroll when dragging node outside the visible area in ScrollPane

南楼画角 提交于 2020-01-25 11:53:29

问题


We have been working on an application that uses a ScrollPane containing an AnchorPane that can grow depending on what nodes get placed on the workspace. We are able to place nodes on the workspace and are able to drag them to different positions using mouse drag events.

What I am now trying to implement is a way to automatically scroll the workspace when we are dragging a node and it touches the edge of the visible workspace. I have been able to detect the point when the node begins to move out of the visible area and I am able to move the scroll bar by setting the vvalue or hvalue depending on whether the node has moved vertically or horizontally. This is working to a certain extent.

When I move a node off to the right and update the hvalue, the workspace is scrolled, the device shifts to the left but the mouse stays where it was meaning that the cursor is now not over the node that is being dragged. I have been trying to find a way to shift the cursor position by the same distance that we have scrolled so that it remains in the same position relative to the node that is being dragged.

I have been searching for information about how to do this and reading the Javadocs to try to find a solution without any success. Can someone please advise how to achieve this? Thanks.


回答1:


I've had a similar problem with a TreeTableView and solved it by detecting DragEnter/DragExit events and starting a TimeLine that will scroll up/down depending on where the DragExit event was detected.

    private void setupScrolling() {
        scrolltimeline.setCycleCount(Timeline.INDEFINITE);
        scrolltimeline.getKeyFrames().add(new KeyFrame(Duration.millis(20), "Scoll", (ActionEvent) -> { dragScroll();}));
        tree.setOnDragExited(event -> {
            if (event.getY() > 0) {
                scrollDirection = 1.0 / tree.getExpandedItemCount();
            }
            else {
                scrollDirection = -1.0 / tree.getExpandedItemCount();
            }
            scrolltimeline.play();
        });
        tree.setOnDragEntered(event -> {
            scrolltimeline.stop();
        });
        tree.setOnDragDone(event -> {
            scrolltimeline.stop();
        });         
    }

Full example: http://programmingtipsandtraps.blogspot.co.at/2015/10/drag-and-drop-in-treetableview-with.html



来源:https://stackoverflow.com/questions/30497775/want-to-trigger-scroll-when-dragging-node-outside-the-visible-area-in-scrollpane

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