JavaFX - 向后拖动节点+ ScrollPane?

时间:2014-12-01 18:27:42

标签: java javafx javafx-8

我有一个JavaFX应用程序,它有一个可滚动的区域。在该可滚动区域中,您可以添加各种类型的可拖动节点。您会认为如果将节点拖向边缘,它将向该方向滚动。但是,它向相反方向滚动。

拖拽的工作原理如下:

        myNode.setOnMousePressed(new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent t){
                originalX1 = 200;
                originalY1 = 200;
                newX1 = ((WebView)(t.getSource())).getTranslateX();
                newY1 = ((WebView)(t.getSource())).getTranslateY();

                System.out.println("(" + newX1 + ", " + newY1 + ")");
        }
    });
    myNode.setOnMouseDragged(new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent t){
                double offsetX = t.getSceneX() - 210;
                double offsetY = t.getSceneY() - 220;
                double newTranslateX = newX + offsetX;
                double newTranslateY = newY + offsetY;

                ((NodeType)(t.getSource())).setTranslateX(newTranslateX);
                ((NodeType)(t.getSource())).setTranslateY(newTranslateY);

                System.out.println("(" + newTranslateX + ", " + newTranslateY + ")");

        }
    });

1 个答案:

答案 0 :(得分:0)

如果您要在ScrollPane中滚动,则应该使用ScrollPane的HvalueVvalue。 作为一种方法,这样的事情:

if (scrollPane.getViewportBounds().getWidth() - t.getSceneX() <= 20) { // Look if the user dragged to the right border
    // Set the horizontal scroll-bar 3% wider
    scrollPane.setHvalue(scrollPane.getHvalue() + 0.03);
} else if (scrollPane.getViewportBounds().getHeight() - t.getSceneY() <= 20) { // Look if the user dragged to the bottom border 
    // Set the vertical scroll-bar 3% wider
    scrollPane.setVvalue(scrollPane.getVvalue() + 0.03);
}
// @TODO Do this for the top and left border too

快乐编码,
Kalasch