steve
steve

Reputation: 1555

How to smoothly scroll SWT composite?

I'm using a SWT ScrolledComposite but when I scroll in Windows I get some tearing / flickering if I scroll to fast. What can I do to double buffer or reduce this effect, or what can I do to override the default scrolling functionality and make it scroll more smoothly? There's text boxes in the scrolling area so I don't think a canvas would work.

Upvotes: 3

Views: 1763

Answers (1)

Vladimir
Vladimir

Reputation: 4850

The trick is to play with delay and use one-pixel scrolling.

Here are parts of the code how I actually do that:

public void scrollOnePixelUp() {
    scrolledComposite.getContent().setLocation(0, scrolledComposite.getContent().getLocation().y - 1);
}

public void scrollOnePixelDown() {
    scrolledComposite.getContent().setLocation(0, scrolledComposite.getContent().getLocation().y + 1);
}

private int pixelScrollDelay = 50;//ms

scrollingThread = new Thread() {
    public void run() {
        doScrolling = true;
        int i = 0;
        while((i < scrollLength) && running && doScrolling) {
            i++;

            if (d.isDisposed())
                return;
            d.asyncExec(new Runnable() {
                public void run() {
                    if (scrollUp)
                        scrollOnePixelUp();
                    else
                        scrollOnePixelDown();
                }                       
            });


            try {
                sleep(pixelScrollDelay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        doScrolling = false;
    }
};

Hope that helps!

Upvotes: 0

Related Questions