Modify the range of a Jslider

Asked

Viewed 115 times

2

In my project I have a Jslider, 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 that it is only possible to select/show even values. 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?

  • I believe it’s setStep(2).

1 answer

0


With the command below:

slider.setMajorTickSpacing(2);

This will set the maximum spacing size between Jslider’s "ticks", but it will still be possible to select non-speed values between them. Adding the excerpt below will display the values of 2 in 2 and restrict the movement of the snap to only walk 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:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.