Modify the range of a JSlider

2

In my project I have a JSlider, that its minimum value is 0 and its maximum value is 10. I would like to modify it so that its interval is two in two numbers, so only being possible to select / display values pairs. At the moment, I'm using the following code to do this:

    int sliderValor = sliderFim.getValue();   

    //se não for par
    if((sliderValor % 2 != 0))
    {
        sliderFim.setValue(++sliderValor);
    }
    lblFim.setText(Integer.toString(sliderValor));

Is there any better or more recommended way to do this?

    
asked by anonymous 15.08.2017 / 02:48

1 answer

0

With the command below:

slider.setMajorTickSpacing(2);

This will set the maximum size of JSlider ticks, but it will still be possible to select non-even values between them. Adding the excerpt below will display the 2-in-2 values and restrict the movement of the snap to just walking on each of the markers:

// define o espaçamento entre os ticks    
slider.setMajorTickSpacing(2);
// desenham os valores dos ticks
// abaixo do slider
slider.setPaintTicks(true);
slider.setPaintLabels(true);
// restringe o snap a parar
// somente sobre os ticks
slider.setSnapToTicks(true);

See working:

    
15.08.2017 / 02:50