java - ScrollBar gets stuck when defining Max and Min -
i trying implement scrollbar
eclipse view , have method setup behavior. when run program, no matter how long list<number> data
is, thumb big scrollbar
, not able move. there doing wrong?
i debugged program , scrollbar
maximum set, thumb fills bar.
private void setupscrollbar(scrollbar scrollbar, list<number> data) { // set max/minimum if (data.size() > 0) { comparator<number> comparator = new comparator<number>() { @override public int compare(number o1, number o2) { return (int) (o2.doublevalue() - o1.doublevalue()); } }; double max, min; data.sort(comparator); max = data.get(data.size() - 1).doublevalue(); min = data.get(0).doublevalue(); scrollbar.setmax(max); scrollbar.setmin(min); scrollbar.setvisibleamount(100 * (max-min)); } else { scrollbar.setmax(0); scrollbar.setmin(0); } }
first of all, custom comparator produces reverse ordered list
, pointed out fabian, because of int
casting 1 == 1.9
, 1.9 == 2.8
1 != 2.8
.
compares 2 arguments order. returns negative integer, zero, or positive integer first argument less than, equal to, or greater second.
to fix this:
- you can use double comparison
data.sort(comparator.comparingdouble(number::doublevalue));
. - or modify
compare
method ofcomparator
instancereturn (int) math.signum(o1.doublevalue() - o2.doublevalue());
the other problem how set visibleamountproperty of scrollbar
: (max-min)
returns full range of scrollbar
, setting property value produces thumb fill 100% available space.
you can fix this, if e.g. set thumb percentage:
final double percentage = 30.0; scrollbar.setvisibleamount((max-min) / 100.0*percentage);
this snippet result in having thumb fill 30% of available space.
Comments
Post a Comment