Gilian Joosen
Gilian Joosen

Reputation: 486

How to click on a javafx slider

I have a question. I am creating an audiplayer, almost everything is finished, but I have a small problem. I can use the slider, but I can only slide it, clicking doesn't work. How can i fix this, i have seen some solutions for JavaFX but not for a javaFX application which uses FXML (I am using FXML). Thank you very much!

Upvotes: 0

Views: 1759

Answers (2)

ed22
ed22

Reputation: 1237

Something like this:

timeSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            timeSlider.setValueChanging(true);
            double value = (event.getX()/timeSlider.getWidth())*timeSlider.getMax();
            timeSlider.setValue(value);
            timeSlider.setValueChanging(false);
        }
});

This would also cause your value property listener to fire if you have any registered. You can register one like this:

timeSlider.valueProperty().addListener(new InvalidationListener() {
        public void invalidated(Observable ov) {
            if (timeSlider.isValueChanging()) {
                //do something here
            }
        }
}

Upvotes: 2

Ginchen
Ginchen

Reputation: 790

Slider has a method setOnMouseReleased(EventHandler<? super MouseEvent> value), which means you can easily add a MouseEvent handler for clicking the slider:

mySlider.setOnMouseReleased((MouseEvent event) -> {
    // do whatever has to be done
});

Upvotes: 2

Related Questions