JavaFX scrolling started and ended

I have a very expensive action that can be done by scrolling the mouse on the panel. I am currently using

pane.setOnScroll({myMethod()}).

The problem is that if you scroll a lot, it calculates everything many times. Therefore, I want to perform my actions only after the scrolling is complete. I was hoping to use setOnScrollStarted, keep the initial value and setOnScrollFinished, to complete my actions.

But I do not know why these two methods are never called. As a test, I used

pane.setOnScroll({System.out.println("proof of action"});

and he was clearly not called.

Any idea on how to call my method only at the end of the scroll?

Thanks in advance, A

+4
source share
1 answer

javadoc ScrollEvent ( ):

(, ), SCROLL_STARTED SCROLL_FINISHED. , SCROLL_FINISHED SCROLL_STARTED touchCount. , SCROLL / .

:

, . , 1 , , 1 ( ), .

public class Main extends Application {
    private Integer scrollCounter = 0;

    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);

            Pane pane = new Pane();
            pane.setOnScroll(e -> {

                scrollCounter ++;

                Thread th = new Thread(() -> {
                    try {
                        Thread.sleep(1000);
                        Platform.runLater(() -> {
                            if(scrollCounter == 1) 
                                System.out.println("Scroll ended!");
                            scrollCounter--;
                        });
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                });
                th.setDaemon(true);
                th.start();
            });

            root.setCenter(pane);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
+2

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


All Articles