JavaFX 2.2: Hooking Slider Drag n Drop Events

后端 未结 3 1732
臣服心动
臣服心动 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:05

    jewelsea's answer was very helpful for setting me on the right track, however if "snapToTicks" is on, undesired behavior results. The "end" value as captured by jewelsea's listener is before the snap takes place, and the post-snap value is never captured.

    My solution sets a listener on value but uses valueChanging as a sentinel. Something like:

    slider.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(
                    ObservableValue observableValue,
                    Number previous,
                    Number now) {
                if (!slider.isValueChanging()
                      || now.doubleValue() == slider.getMax()
                      || now.doubleValue() == slider.getMin()) {
                    // This only fires when we're done
                    // or when the slider is dragged to its max/min.
                }
            }
        });
    

    I found that checking for the max and min value was necessary to catch the corner case where the user drags the slider all the way past its left or right bounds before letting go of the mouse. For some reason, that doesn't fire an event like I'd expect, so this seems like an okay work-around.

    Note: Unlike jewelsea, I'm ignoring the starting value for the sake of simplicity.

    Note 2: I'm actually using ScalaFX 2, so I'm not sure if this Java translation compiles as-written.

提交回复
热议问题