How to make JavaFX Slider to move in discrete steps?

前端 未结 2 1865
旧时难觅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 04:06

    In FXML:

    
    

    Or in Java:

    Slider slider = new Slider(1, 4, 1);
    slider.setBlockIncrement(1);
    slider.setMajorTickUnit(1);
    slider.setMinorTickCount(0);
    slider.setShowTickLabels(true);
    slider.setSnapToTicks(true);
    

    The key here is the snap to ticks option combined with a proper combination of tick units. This setting results in the following slider which can only be used to select values ranging from 1 to 4 :

提交回复
热议问题