Grab mouse position inside scroll pane

Asked

Viewed 91 times

1

How do I get the mouse position inside a scroll pane 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 any more pieces of Có just ask, but I think I posted what needed understanding to be clear now.

Onde foi dropado e a posição do vbar Onde foi dropado e a posição do vbar

public EventHandler<? super MouseEvent> getStartClassDrag(Node n, TipoClasse t) {
    return (e) ->{
        //this.ac_comp.setDisable(true);
        ClipboardContent cc = new ClipboardContent();
        cc.putUrl(FXMLFile.CLASSE);
        n.startDragAndDrop(TransferMode.ANY).setContent(cc);

        //Trecho de código original
        //sp_desktop.onDragDroppedProperty().set((evet) -> {

        //Solução
        pn_desktop.onDragDroppedProperty().set((evet) -> {
            if(evet.getDragboard().getUrl().equals(FXMLFile.CLASSE)) {
                FXMLLoader loader = new FXMLLoader(getClass().getResource(FXMLFile.CLASSE));
                try {
                    loader.load();
                } catch (Exception err) { err.printStackTrace(); }
                ClasseController controller = loader.getController();
                controller.inicializar(this);
                controller.classe.setTipo(t);
                classes.add(controller);

                Point2D teste = new Point2D(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));
}
  • Our language is the Português, translate your question.

1 answer

2


I have an example I made recently to move my window. Check if the code below helps in 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);
    });
  • Thanks for answering. I ended up discovering that the problem was the knot in which the Istener was. As you can see, in the example I posted it is in the scroll pane, when it should be in the pane located inside the scroll pane. Still thanks for taking time out of your day to try and help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.