4

I have just realised that JSlider cannot deal with floating point numbers. Can anybody recommend a Swing/AWT alternative that can?

EDIT: Or a workaround of some description.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jack H
  • 2,440
  • 4
  • 40
  • 63

1 Answers1

7

Sliders in general deal with ranges of numbers. From a practical implementation, each slider must have two elements:

  1. A starting position.
  2. A finite number of "next" increments.

It is the "finite number" that is causing you the trouble. Without a finite number of increments, the slider cannot fit on a screen. With a finite number of increments, it is impossible to select a float number that lies between two incremental "steps".

In short, it is impossible; so, here's the workaround:

  1. Decide on the range of the "float" side of the input. This could be 0.0f to 10.0f or whatever, it doesn't matter, but you must have a range.
  2. Decide on the smallest increment you wish to support. This could be 0.1f or 0.001f or whatever, it doesn't matter, but you must have an increment.
  3. Create a pair of functions. One that takes the Slider integer value and "maps" them to the float value, and one that takes the float value and "maps" them to a Slider value.

An example, for 5.0f to 10.0f with 0.1f increments:

((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);
}
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 2
    What you've described is basically [Fixed Point Math](http://en.wikipedia.org/wiki/Fixed-point_arithmetic). A tried and true solution for this type of problem. – Jason Braucht Apr 17 '12 at 19:01
  • 1
    @VisionIncision Glad to hear it. Jason is right, this is fixed-point math, and in some sense all of computing is discrete mathematics, However, sometimes an explanatory guide along the path of solution is more approachable than an initial foray into the mathematics. I too recommend you look into fixed point math at your convenience, as it will be used again and again (and actually floating point numbers _are_ fixed point, but with a very small resolution). – Edwin Buck Apr 17 '12 at 19:12
  • 1
    I agree Edwin, the example given in your answer was both useful and appropriate. My comment was mainly intended simply to give a name to your solution. – Jason Braucht Apr 17 '12 at 19:46
  • 2
    1+ for this very good discussion. Don't forget though that you can make the JSlider look as if it is dealing with floating point numbers by setting the label table for the slider to display floating point number Strings. The key method to use is `setLabelTable(Dictionary labels)`. – Hovercraft Full Of Eels Apr 17 '12 at 19:58
  • Hi, I think I must be doing something wrong, as this line Slider.setValue((value - 5.0f)/0.1f); still gives an error. setValue() expects an integer. Thanks, also, the name for your solution was very useful. Good further reading. – Jack H Apr 17 '12 at 20:15
  • You need to actually convert the float to an int. I'll update the answer – Edwin Buck Apr 17 '12 at 20:41
  • I don't see why sliders in general need to deal with a fixed set of values. You could have a slider with a value output depending on the position. So the slider is only constrained by its pixel width. Unfortunately, I haven't seen a good implementation of that so far... – brimborium Mar 24 '14 at 11:38