views:

530

answers:

4

I have an application that displays a ScrolledComposite. Users are complaining that the horizontal scrolling increment is too fine, i.e. each click on the horizontal scroll arrow currently moves the bar one pixel at a time. They want individual clicks to cause greater horizontal movement. Could someone explain how I could implement this? A snippet of the code follows:

ScrolledComposite myScrolledComposite = new ScrolledComposite(parent, 
        SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);

if ( myScrolledComposite == null) {
    throw new NullPointerException("ScenePreView.java:  " + 
            "Method createPartControl()  " + 
            "ScrolledComposite myScrolledComposite == null."); 
}

Composite myComposite = new Composite(myScrolledComposite, SWT.NONE);

if ( myComposite == null) {
    throw new NullPointerException("ScenePreView.java:  " + 
            "Method createPartControl()  " + 
            "Composite myComposite == null."); 
}

myScrolledComposite.setContent(myComposite);
A: 

Use the setIncrement() method of the SWT Scrollbar class. This lets you modify the amount by which the scroll region moves when the arrow buttons are pressed. See API Reference for SWT

Nicholas Smith
Thank You. I looked in the ScrolledComposite class, but didn't think to look in the Scrollbar class.
Dr. Faust
+1  A: 

Get the scrollbar from the composite and set it's increment.

myScrolledComposite.getVerticalBar().setIncrement(10);

Wayne Beaton
Thanks. That did the trick.
Dr. Faust
A: 

I know it's more than you asked for, but I highly recommend checking out the SWT animation toolkit which has an implementation of smooth scrolling. It will make your application way cooler.

You can check it out and see the sources - it's a good example (yet, a bit advanced) of how to play with the scrolling.

zvikico
I'll check it out. Thanks.
Dr. Faust
+1  A: 

This should work pretty well and provide usable defaults. It should update automatically when the control inside the ScrolledComposite is resized.

myComposite.addControlListener( new ControlAdapter() {
    @Override
    public void controlResized( ControlEvent e ) {
        ScrollBar sbX = scrolledComposite.getHorizontalBar();
        if ( sbX != null ) {
            sbX.setPageIncrement( sbX.getThumb() );
            sbX.setIncrement( Math.max( 1, sbX.getThumb() / 5 ) );
        }
    }
});
derBiggi
Thanks. Works great.
Dr. Faust