JSlider Alternative

I just realized that JSlider cannot deal with floating point numbers. Can anyone recommend an alternative to Swing / AWT that can?

EDIT: or a workaround for some description.

+3
source share
1 answer

Sliders generally deal with ranges of numbers. From the practical implementation, each slider should have two elements:

  • Starting position.
  • The final number of "subsequent" increments.

This is the "final number" that is causing you problems. Without a finite number of increments, the slider cannot fit on the screen. With a finite number of increments, it is impossible to select a number floatthat is between two incremental "steps".

, ; , :

  • "" . 0.0f 10.0f - , , .
  • , . 0.1f 0.001f - , , .
  • . , Slider "" , , float "" Slider.

: 5.0f 10.0f 0.1f:

((10.0f - 5.0f) / 0.1f) + 1 = 51 increments (0 to 50)

updateSlider(float value) {
  if (value > 10.0f) {
    Slider.setValue(50);
  } else if (value < 5.0f) {
    Slider.setValue(0);
  } else {
    Slider.setValue((int)Math.round((value - 5.0f)/0.1f));
  }
}

float updateFloat(Slider slider) {
  int value = slider.getValue();
  return 5.0f + (slider.getValue() * 0.1f);
}
+7

All Articles