Java SWT Slider.getMaximum() equals 100, but you can only drag it up to 90
Asked Answered
E

3

6

If you create a Slider (org.eclipse.swt.widgets.Slider) then call getMaximum() on it, the value is 100. But when you actually try dragging the Slider to its maximum value, it only reaches 90.

I can work round this problem:

sl.setSelection(sl.getMaximum());   // sl.getMaximum() is 100   
int actualMax = sl.getSelection();  // should be 100, but is actually 90

But something definitely seems to be wrong, no?

Etalon answered 15/12, 2012 at 11:57 Comment(1)
Check the width of the slider (the increment). I'm guessing that the slider stops before the maximum to include the increment.Leacock
T
5

As you know Slider is nothing but ScrollBar widget,the maximum value that you set is equal to max_value+thumb_value value.

Try this code:

  final Slider slider = new Slider(shell, SWT.NONE);
  slider.setMaximum(100);
  slider.setMinimum(0);
  slider.setThumb(20);

  slider.addSelectionListener(new SelectionListener() {

    @Override
    public void widgetSelected(SelectionEvent e) {
        System.out.println( slider.getSelection()  +"   "+ slider.getThumb());
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {

      // TODO Auto-generated method stub

    }
  });
Thermoelectricity answered 17/12, 2012 at 20:26 Comment(1)
I didn't know that. But thank you. So the maximum value you can actually drag the slider to is sl.getMaximum() - sl.getThumb(). I still think it would be much more natural for the maximum value you can drag it to to be sl.getMaximum().Etalon
A
1

I can also observe this behavior on Windows 7 with SWT 3.6.1 and SWT 4.2.1.

A simple but somehow dissatisfying workaround would be to use:

sl.setMaximum(110);

Then the values range from 0 to 100.

Ammadas answered 17/12, 2012 at 10:4 Comment(2)
This does not work in general (since it depends on the thumb value - see my answer for details).Satyr
Also, the middle of the slider will now be 55.Udelle
S
1

Although the provided answers helped to solve the problem, it still took a while for me to implement it correctly. If you want to make the values 0-100 selectable with your slider, you should do:

// we want to have the values 0-100
slider.setMaximum(100 + slider.getThumb());
// this now returns the selected value, which can be from 0-100
slider.getSelection();
Satyr answered 27/6, 2014 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.