Pick up mouse position within scroll pane

1

How can I get the mouse pointer inside a scroll pan in javafx? I have an app that does a drag and drop operation inside it, and I need that information to position the node on the screen.

The scroll pane is Pannable.

If you need to see some more of it, just ask, but I think I've posted what is necessary for the understanding to be clear already.

publicEventHandler<?superMouseEvent>getStartClassDrag(Noden,TipoClasset){return(e)->{//this.ac_comp.setDisable(true);ClipboardContentcc=newClipboardContent();cc.putUrl(FXMLFile.CLASSE);n.startDragAndDrop(TransferMode.ANY).setContent(cc);//Trechodecódigooriginal//sp_desktop.onDragDroppedProperty().set((evet)->{//Soluçãopn_desktop.onDragDroppedProperty().set((evet)->{if(evet.getDragboard().getUrl().equals(FXMLFile.CLASSE)){FXMLLoaderloader=newFXMLLoader(getClass().getResource(FXMLFile.CLASSE));try{loader.load();}catch(Exceptionerr){err.printStackTrace();}ClasseControllercontroller=loader.getController();controller.inicializar(this);controller.classe.setTipo(t);classes.add(controller);Point2Dteste=newPoint2D(evet.getX()+sp_desktop.getHvalue(),evet.getY()+sp_desktop.getVvalue());System.out.println(sp_desktop.getHvalue()+"x" + sp_desktop.getVvalue());

                teste = sp_desktop.parentToLocal(teste);

                relocateToPoint(teste.getX(), teste.getY(), controller.ap_classe);
                controller.ap_classe.setVisible(true);

            }
        });

        //Trecho de código original
        //sp_desktop.onDragOverProperty().set((evento) -> {

        //Solução
        pn_desktop.onDragOverProperty().set((evento) -> {
            if(evento.getDragboard().getUrl().equals(FXMLFile.CLASSE)) {
                evento.acceptTransferModes(TransferMode.ANY);
            }
            evento.consume();
        });

        n.onDragDoneProperty().set((aux) ->{
            sp_desktop.onDragDroppedProperty().set(null);
            sp_desktop.onDragOverProperty().set(null);
            ac_comp.setDisable(false);

        });
    };
}

protected void relocateToPoint(double x, double y, AnchorPane no) {
    no.setVisible(true);
    pn_desktop.getChildren().remove(no);
    pn_desktop.getChildren().add(no);
    no.setLayoutX(x - (no.getWidth() / 2));
    no.setLayoutY(y - (no.getHeight() / 2));
}
    
asked by anonymous 27.08.2018 / 03:47

1 answer

2

I have an example I recently made to move my window. Check if the code below helps with the solution.

    double xOffset = 0;
    double yOffset = 0;

    scrollPane.setOnMousePressed((MouseEvent event) -> {
        // Nesse trecho de código será capturado a posição do mouse
        xOffset = event.getSceneX();
        yOffset = event.getSceneY();
    });
    scrollPane.setOnMouseDragged((MouseEvent event) -> {
        primaryStage.setX(event.getScreenX() - xOffset);
        primaryStage.setY(event.getScreenY() - yOffset);
    });
    
27.08.2018 / 14:34