Bassusour
Bassusour

Reputation: 175

MouseDragEvent not called

I want to be able to read my sliders value, and therefore I use an event listener for my controller class:

//Controller class
@FXML
    private Slider gridSlider;

@FXML
    void sliderChange(MouseDragEvent event) {
        int sliderValue = (int) gridSlider.getValue();
        System.out.println(sliderValue);
    }

//My FXML class
<Slider fx:id="gridSlider" blockIncrement="1.0" layoutX="203.0" layoutY="84.0" majorTickUnit="8.0" max="32.0" min="8.0" minorTickCount="7" nodeOrientation="LEFT_TO_RIGHT" onMouseDragReleased="#sliderChange" prefHeight="38.0" prefWidth="180.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="8.0">

I have tried every single MouseDragEvent and none of the MouseDragEvents has actually been called (like on drag detected which should have been called the moment i start dragging the slider). I found this post, but it didn't solve my problem. JavaFX mouse drag events not firing

Upvotes: 0

Views: 198

Answers (1)

Slaw
Slaw

Reputation: 45736

The MouseDragEvent is associated with "full press-drag-release" gestures. Such gestures have to be explicitly started by calling Node#startFullDrag() inside a DRAG_DETECTED handler. The documentation of MouseEvent provides more information about the different dragging gestures.

With that being said, you probably don't want to use mouse events to know when your Slider's value has changed. It would be listen to one or more properties of your Slider. For instance, you can listen or bind to the value property to always know the current value. There's also the valueChanging property:

When true, indicates the current value of this Slider is changing. It provides notification that the value is changing. Once the value is computed, it is reset back to false.

That property will be true while the user is dragging the thumb and will become false once the user "drops" the thumb. To listen to the property while using FXML you can inject the Slider into the controller and add a listener to it in the initialize method:

public class Controller {

  @FXML private Slider slider;

  @FXML
  private void initialize() {
    slider.valueChangingProperty().addListener((obs, ov, nv) -> {
      if (!nv) {
        // do something...
      }
    });
  }
}

If you want, however, you can add such a listener via the FXML file—see Introduction to FXML:

<StackPane ...>
  <Slider onValueChangingChange="#handleValueChangingChange"/>
</StackPane>
public class Controller {

  @FXML
  private void handleValueChangingChange(ObservableValue<Boolean> obs, Boolean ov, Boolean nv) {
    if (!nv) {
      // Do something...
    }
  }
}

Note: My examples use the valueChanging property because that would seem to provide the behavior you want, as you were trying to listen to when the mouse was released. However, the value can be changed programmatically which, as far as I know, does not affect the valueChanging property. Thus, if you want to always know the current value, you need to observe the value property.

Upvotes: 3

Related Questions