JavaFX 2.2: Hooking Slider Drag n Drop Events

后端 未结 3 1726
臣服心动
臣服心动 2020-12-07 01:22

I am trying to catch the events on the JavaFX Slider especially the one which indicates that the drag stopped and was released. At first I used the valueProperty

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 02:13

    There could be times when you want to know when the user is moving the slider versus the slider value changing due to a binding to a property. One example is a slider that is used on a media player view to show the media timeline. The slider not only displays the time but also allows the user to fast forward or rewind. The slider is bound to the media player's current time which fires the change value on the slider. If the user moves the slider, you may want to detect the drag so as to stop the media player, have the media player seek to the new time and resume playing. Unfortunately the only drag event that seems to fire on the slider is the setOnDragDetected event. So I used the following two methods to check for a slider drag.

        slider.setOnDragDetected(new EventHandler() {
            @Override
            public void handle(Event event) {
                currentPlayer.pause();
                isDragged=true;     
            }
        });
    
    slider.setOnMouseReleased(new EventHandler() {
            @Override
            public void handle(Event event) {
                if(isDragged){
                    currentPlayer.seek(Duration.seconds((double) slider.getValue()));
                    currentPlayer.play();
                    isDragged=false;
                }       
            }
        });
    

提交回复
热议问题