Sacrilege – a Custom SWT Scrollbar

Home  >>  Eclipse  >>  Sacrilege – a Custom SWT Scrollbar

Sacrilege – a Custom SWT Scrollbar

On December 17, 2014, Posted by , In Eclipse, By ,, , With 5 Comments

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):

SWT Scrollbar: FlatScrollBar

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:

SWT Scrollbar: Slider Mimicry

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.

SWT Scrollable: Table Scrollbar Overlay

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.

 

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:

http://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.

Follow me

Frank Appel

Frank is a seasoned programmer, team leader, trainer, author and presenter with two decades of experience in professional software development.

Specialized in the Java language and focused on quality-oriented agile paradigms he is a stalwart of test-driven practices in particular.

He understands the creation process of software as a craftsmanship based on a well-balanced mix of knowledge and the experience of the daily work.

fappel@codeaffine.com
Follow me

5 Comments so far:

  1. Lars Vogel says:

    Awesome Frank, this is a long outstanding request for the Eclipse platform. Especially on a dark themed Eclipse the scrollbar looks very bad. I opened Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=455526 to ask SWT for API to exchange the native ScrollBar.

    • Frank Appel says:

      Thanks Lars for the praise;-) Of course it would be great if the custom scrollbar could be registered with Scrollable directly. Will be interesting to see how the bug evolves

  2. […] With the possibility to use a custom scrollbar implementation, it is possible to style a NatTable completely with a dark theme. As an example for a stylable scrollbar we use the FlatScrollBar from Code Affine. […]

  3. I am using Eclipse and an ant build script. How do I download a jar file for the flatscrollbar?

    I did install new software using the http://fappel.github.io/xiliary url, but that did not seem to do something useful

Leave a Reply

Your email address will not be published. Required fields are marked *