How to scroll in ScrollPane with the mouse?

Is it possible to move vertically and horizontally without using the scrollbar in ScrollPane. I want to scroll when the left mouse button is pressed, and the mouse moves right, left, up and down. Should I add a listener or something like that? thanks in advance.

+4
source share
2 answers

Just call

scrollPane.setPannable(true);

or if you use FXML

<ScrollPane pannable="true" ... >
+9
source

I found an error in "setPannable (true)" that is not very smooth if the two drag and drop times are short.

How to solve it?
You need to manually use events.

    private boolean dragged = false;


    imageView.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
        dragged = true;
    });

    imageView.addEventFilter(MouseEvent.MOUSE_RELEASED, event -> {
        dragged = false;
    });

    imageView.addEventFilter(MouseEvent.MOUSE_DRAGGED, event -> {
        if (!dragged) {
            event.consume();// make the drag smooth
        }
    });

    scrollPane.setContent(imageView);
    scrollPane.setPannable(true);
0
source

Source: https://habr.com/ru/post/1547990/


All Articles