0
votes

I am trying to update some information every time the Slider value is changed, so I have the code set up like this:

int betAmount = 0;
Slider betSlider = new Slider();
betSlider.setMinorTickCount(4);
betSlider.setMajorTickUnit(250);
betSlider.setSnapToTicks(true);

betSlider.valueChangingProperty().addListener((obs, wasChanging, isChanging) -> {
    if (!isChanging) {
        betAmount = (int) betSlider.getValue();
        update(); //update method, not relevant to problem
    }
});

The problem that I am having is that the getValue() method on my slider is being called before it snaps to the nearest tick. Because of this, I am getting the incorrect value stored in my betAmount variable. I was wondering if there was any way to get the slider's value after it has finished snapping to the nearest tick.

2

2 Answers

0
votes

Try using valueProperty() in place of valueChangingProperty()

betSlider.valueProperty().addListener((obs, oldValue, newValue) -> {
       betAmount = newValue.intValue());
});

valueChanging -> It provides notification that the value is changing.

value -> The current value represented by this Slider.

0
votes

The way I've done this in the past is to use the slider's valueProperty() and filter out any new values that occur while the slider is still changing.

int betAmount = 0;
Slider betSlider = new Slider();
betSlider.setMinorTickCount(4);
betSlider.setMajorTickUnit(250);
betSlider.setSnapToTicks(true);

betSlider.valueProperty().addListener((obs, oldValue, newValue) -> {
    if(newValue != null && !newValue.equals(oldValue) && !betSlider.isValueChanging()) {
        betAmount = newValue.intValue();
        update(); //update method, not relevant to problem
    }
});