Sacrilege – a Custom SWT Scrollbar
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.
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.
- Xmas Clean Sheet Update (0.9) - 21. December 2021
- Clean Sheet Service Update (0.8) - 23. May 2020
- Clean Sheet Service Update (0.7) - 24. April 2020
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.
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
[…] 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. […]
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
Sarah,
the URL points to a P2 repository. For development you should add it to your target definition (http://www.vogella.com/tutorials/EclipseTargetPlatform/article.html). It’s a long time ago I worked with P2 and ant builds, but maybe you find this link https://wiki.eclipse.org/Equinox/p2/Ant_Tasks useful.
[…] (Windows 7) und auch Scrollbars (Linux und Windows). Für das letztgenannte Problem hat sich Frank Appel eine Lösung ausgedacht, in der Tree- und Table-Widgets in einem Composite gewrappt und eigene Scrollbars gerendert werden […]
Hi Frank,
My SWT project developed with Eclipse uses Maven for dependent libraries.
How can I do to use your FlatScrollBarTable in my project ?
Thanks!
Hi,
thanks for evaluating the FlatScrollBar Table and sorry for answering that late, but I was on vacation. Unfortunately, I didn’t find the time to publish the FlatScrollBar stuff on Maven Central, shame on me. I know it shouldn’t be that much effort, so I’ve opened an issue on GitHub regarding this problem: Publish SWT FlatScrollbar Libraries on Maven Central. For the time being, the library is available as download packed in the http://fappel.github.io/xiliary/ p2 repository only. The following link shows the location where the current version can be retrieved from the repositorie’s gh-pages branch: current xiliary libraries and sources.
Regards,
Frank
This will hopefully soon work on JDK9+ as well! I submitted a patch here: https://github.com/fappel/xiliary/pull/91 :)
Impressive. Most impressive. Just had a look and merged your changes into the development branch. The travis build on that branch is green. However, the subsequent OSX branch build is not triggered automatically. This has nothing to do with your fix, but it is needed to run a complete test build with P2 repository deployment on the pages branch. Unfortunately, the Travis build log is not available (might be related to issue https://github.com/travis-ci/travis-ci/issues/7443) so I can’t find the cause of that problem in a quick way. Hopefully, I’m able to fix this in the next days, so the scroll bars will be available for everyone using a newer JDK version. Thank you very much!