Desktop Java

SWT Look and Feel: Customize FlatScrollBar Color and More

Recently I introduced a custom slider control useful for improving the SWT look and feel of more subtle view layouts. Happily it seems the widget already found early adopters outside of the Code Affine world. Which led to some enhancements that will be presented in the following sections.
 
 
 
 
 
 
 
 
style-scrollbar

SWT Look and Feel of Scrollbars

‘SWT is a thin abstraction layer on top of native OS widgets. This 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’ (to quote myself from Sacrilege, …)

In particular I perceive the native SWT scrollbars often as disruptive. But being part of native controls their SWT look and feel is not customizable at all. Hence I created a custom SWT slider widget called FlatScrollBar.

As it is also not feasible to replace the native SWT scrollbar of controls like trees or tables, I introduced a ScrollableAdapter that works around this limitations with overlays. So far I have adapters for tree and tables available and a prototype for ScrolledComposite as work in progress.

Color …

When it comes down to styling, colors are very important. So it is not surprising that the first enhancement request was about the ability of changing the colors of the various slider elements. Because of this I introduced the three properties incrementColor, pageIncrementColor and thumbColor to allow apropriate adjustments:

The following snippet…

FlatScrollBarTable table = [...]
table.setPageIncrementColor( getColor( SWT.COLOR_INFO_BACKGROUND ) );
table.setThumbColor( getColor( SWT.COLOR_WIDGET_LIGHT_SHADOW ) );

… renders the scrollbars like this (getColor retrieves the system color from the current display):

scrollbar-color

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. However the FlatScrollBar control itself works well on OS X too.

…and More

The attentive reader might wonder what the incrementColor is useful for as the picture shows only three slider elements. It is possible to display increment buttons using the incrementButtonLength:

FlatScrollBarTable table = [...]
table.setIncrementButtonLength( 7 );
table.setIncrementColor( getColor( SWT.COLOR_WIDGET_DARK_SHADOW ) );
[...]

increment-button

Neat, isn’t it?

The last enhancement is not style related but nevertheless noteworthy. It adds untyped event handling to the FlatScrollBar. So if you happen to use Java 8, scrollbar selection observation can be handled as shown here:

FlatScrollBar scrollBar = new FlatScrollBar( parent, SWT.HORIZONTAL );
scrollbar.addListener( SWT.Selection, event -> handleEvent() );

 

Conclusion

Using the FlatScrollBar and the ScrollableAdapter in one of our projects looks promising so far. 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. The controls are part of the com.codeaffine.eclipse.swt feature of the Xiliary P2 repository: 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

Reference: SWT Look and Feel: Customize FlatScrollBar Color and More from our JCG partner Rudiger Herrmann at the Code Affine blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button