Stabilized Scrollbars

Stabilized Scrollbars

Numerous awt Components provide their own Scrollbars when needed. However, when you first employ one explicitly, perhaps for adjusting colors, you may encounter a frustrating problem. On some platforms the Scrollbar moves as expected as you drag it with the mouse, but jumps back to its original position when the mouse button is released. You can overcome this problem in the listener that you provide for the Scrollbar’s events:

 public void adjustmentValueChanged(            AdjustmentEvent ev) {    Component co = ev.getSource();    if (co instanceof Scrollbar) {        Scrollbar sb = (Scrollbar)co;        sb.setValue(ev.getValue());    }// Your other code here}

Surprisingly this is all it takes to stabilize Scrollbars on all platforms. If you have no listener for changes in scrolled values, you could extend the Scrollbar class itself with the above method.


Share the Post: