Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swt

slider

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?

like image 654
user1310503 Avatar asked Dec 15 '12 11:12

user1310503


3 Answers

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

    }
  });
like image 151
sambi reddy Avatar answered Oct 04 '22 22:10

sambi reddy


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.

like image 28
Baz Avatar answered Oct 04 '22 21:10

Baz


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();
like image 20
Simon Avatar answered Oct 04 '22 22:10

Simon