SWT is a thin abstraction layer on top of native OS widgets. Which is a very good thing if you intent that your applications integrate well with the OS look and feel. But as a trade-off this approach limits styling capabilities significantly.

In particular I perceive the native SWT scrollbar often disruptive on more subtle view layouts. Coming across this problem recently I gave a custom SWT scrollbar widget a try. This post introduces the outcome – a simple slider control, usable as SWT Slider replacement or Scrollbar overlay.

SWT Scrollbar

The OS scrollbar abstraction of SWT has two manifestations: org.eclipse.swt.widgets.Scrollbar and org.eclipse.swt.widgets.Slider . The differences between both widgets are explained in the following JavaDoc passage:

‘Scroll bars are not Controls. On some platforms, scroll bars that appear as part of some standard controls such as a text or list have no operating system resources and are not children of the control. For this reason, scroll bars are treated specially. To create a control that looks like a scroll bar but has operating system resources, use Slider.’

This means the Slider provides at least a minimum of programmtical influence, like setting its bounds. But derivates of org.eclipse.swt.widgets.Scrollable (the superclass of all controls that have standard scroll bars) just provide the read-only abstraction Scrollbar .

Which still is very useful to react to scroll events for example, but leaves practically no room for look and feel adjustments. And the application range of sliders is usally limited to custom components, that – for whatever reasons – cannot use the scrollbars provided by the Composite super class.

FlatScrollBar

Although there were some cross platform obstacles to overcome, creating a custom slider was straight forward. The following picture shows the native slider on the left shell in comparison to the FlatScrollBar control used on the right shell (OS: Windows 7):

It is noteworthy that the custom slider expands on mouse over as shown by the vertical bar. The horizontal bar depicts the compact base apprearance as a discreet thumb and selection indicator. In general the FlatScrollBar mimics essentially behavior, semantics and API of a Slider/Scrollbar :

Obviously I decided to omit the arrow up and down buttons, but this is just an optical adjustment. While not configurable yet, the arrow buttons can be revived by changing a single constant value in the source code.

ScrollableAdapter

But what about the scrollbars of Scrollable derivates like text, tree, tables or the like? Being part of the OS control itself as stated above, they are simply not replaceable. Theoretically one could deactivate scrolling and use some kind of custom scrolled composite to simulate scrolling behavior. But this has several downsides. I gave this approach a try and the results were not satisfying.

However wrapping a scrollable into an overlay adapter-composite seems more promising. So far I was able to adapt successfully to Tree and Table controls.

And this is how adapter creation looks like:

new FlatScrollBarTable( parent, ( adapter ) -> new Table( adapter, SWT.NONE ) );

Easy enough, isn’t it? The second parameter is a generic factory ( ScrollableFactory<T extends Scrollable> ) that allows to adapt to various scrollable types. But as a generic overlay implementation is not possible at all, for now only trees and tables adapters are available.

The adapter provides access to the table instance by the method FlatScrollBarTable#getTable() . This allows to adapt also to JFace tree- and table-viewers without a problem.

As native scrollbars on Mac OS look acceptable out of the box the adapter refrains from custom overlays on that platform. Only Gtk and MS Windows platforms are affected. Hence no Mac Screenshot in the title image. However the FlatScrollBar control itself works well on OS X too. As native scrollbars on Mac OS look acceptable out of the box the adapter refrains from custom overlays on that platform. Only Gtk and MS Windows platforms are affected. Hence no Mac Screenshot in the title image. However the FlatScrollBar control itself works well on OS X too.

Conclusion

Using the FlatScrollBar and the ScrollableAdapter in one of our projects looks promising so far. Of course the code base is pretty new and might contain some undetected issues or flaws. However I found it worthwhile to introduce this controls to an outside audience, which might help to reveal such flaws or lead to further requirements.

I am curious to see how sustainable this approach will be and if it is possible to adapt also to text and/or styled text controls. If you want to check out the controls, they are part of the com.codeaffine.eclipse.swt feature of the Xiliary P2 repository available at:

https://fappel.github.io/xiliary

In case you want to have a look at the code or file an issue you might also have a look at the Xiliary GitHub project. Look for FlatScrollbarDemo , FlatScrollBarTreeDemo and FlatScrollBarTableDemo for usage examples:

https://github.com/fappel/xiliary

For everything else feel free to use the commenting section below.