How to make JavaFX Slider to move in discrete steps?

前端 未结 2 1849
旧时难觅i
旧时难觅i 2021-01-04 03:18

I am making a GUI using JavaFx and I need sliders that only allow integers to ever be selected.

I know I can use snapToT

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 03:56

    You can simply add a listener to the valueProperty of the Slider and then you can either set the integer value of the new Number value:

    slider.valueProperty().addListener((obs, oldval, newVal) -> 
        slider.setValue(newVal.intValue()));
    

    or alternatively you can use integer rounding using Math.round:

    slider.valueProperty().addListener((obs, oldval, newVal) ->
        slider.setValue(Math.round(newVal.doubleValue())));
    

提交回复
热议问题