-1

I'm trying to modified this JSLider function

  public JSlider getSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) {
        JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init);
        slider.setPaintTicks(true);
        slider.setMajorTickSpacing(mjrTkSp);
        slider.setMinorTickSpacing(mnrTkSp);
        slider.setPaintLabels(true);
        slider.addChangeListener(new SliderListener());
        return slider;
      }

I would like to set a range of min 0 and max 1 but I want to be able to return 0.1 , 0.2 , 0.3 to maximum 1 ( depends on the user slide value )

So I tried sliderR = getSlider(0, 1, 0, 50, 5);

Unfortunately this will only return 0 OR 1 , but I want to be able to return 0 , 0.1 0.2 0.3 etc..

Thank you

napi15
  • 2,354
  • 2
  • 31
  • 55
  • 1
    Consider using a `JSpinner` with a `double` based [`SpinnerNumberModel`](http://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerNumberModel.html#SpinnerNumberModel-double-double-double-double-) instead. – Andrew Thompson Jan 19 '17 at 20:11
  • 1
    @AndrewThompson I was thinking about that , but I will forced to cast a lot of things witch won't be ideal – napi15 Jan 19 '17 at 20:12
  • *"but I will forced to cast a lot of things"* Why? Are you aware of `SpinnerNumberModel.getNumber().doubleValue()`? – Andrew Thompson Jan 19 '17 at 20:14
  • @AndrewThompson Thank you for elaborating , but some boxes are expecting floats and there are many attributes initialized in floats so I will probably have to try casting (float) SpinnerNumberModel.getNumber().doubleValue() , wich might work – napi15 Jan 19 '17 at 20:20
  • 1
    No need for casting, but good time for checking the documentation.. `SpinnerNumberModel.getNumber().floatValue()` – Andrew Thompson Jan 19 '17 at 20:36

1 Answers1

1

My idea is the following: instead of stepping by 0.1 from 0 to 1, step by 1 from 0 to 10. Then, simply divide the value by 10 e.g. in the ChangeListener or whereever you need that value.

Marc Scheib
  • 1,963
  • 1
  • 24
  • 29