Set ScrolledComposite's ScrollBar visibility to false in SWT

966 views Asked by At

I have two ScrolledComposites and I'm synchronizing their vertical scroll positions like this:

final ScrollBar vScroll1 = canvasScroll.getVerticalBar();
final ScrollBar vScroll2 = titleScroll.getVerticalBar();

vScroll1.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        titleScroll.setOrigin(titleScroll.getOrigin().x, canvasScroll.getOrigin().y);
    }
});

vScroll2.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        canvasScroll.setOrigin(canvasScroll.getOrigin().x, titleScroll.getOrigin().y);
    }
});

This works fine, except it shows the scrollbars for both ScrolledComposites. I only want one ScrolledComposite's scrollbars to be visible, so I set one of their visibilities to false:

vScroll2.setVisible(false);

This has no effect. I also tried to instantiate the ScrolledComposite without the SWT.V_SCROLL flag, but this results in a null pointer exception when running the above code. The scrollbar does need to be there, I just want it to be invisible. Is that possible?

2

There are 2 answers

3
Baz On BEST ANSWER

The simple answer is: "No".

If you create the ScrolledComposite without SWT.H_SCROLL or SWT.V_SCROLL, it cannot be scrolled, i.e. calling setOrigin(Point) or related methods simply won't do anything.

If on the other hand, you enable scrollbars, but want to hide them, the OS will just override your decision. The scroll bars are controlled by the OS, meaning that the OS will decide if they are visible or not. Calling setVisible(false) on a ScrollBar is nothing more than a hint to the OS. It might follow it or not...

Sorry to be the bearer of bad news :\


You could try wrapping the ScrolledComposite in another Composite and forcing this one to "crop" the scrollbar of the contained ScrolledComposite (by changing its size), but this is more of a hack.

0
PressOK On

This post is old (2014) but I had to weigh in here because the accepted answer is totally wrong. In addition to the following there are probably other ways to achieve this. Answers like the one above are worse than not replying at all because they are misleading.

If you call [YourScrolledComposite].getHorizontalBar().setVisible(false) inside of a listener that is processed after the OS adds the scrollbar, it will be (re-)hidden. This happens before the draw (or in the paint event), so in effect the scrollbar is never shown. This allows you to keep the ScrolledComposite with SWT.H_SCROLL (or vertical) flags, and you can still navigate the contents using [YourScrolledComposite].setOrigin(...).