March 21st, 2008

Auto-scrolling in Swing applications

Auto-scrolling is a feature that can be found in most mainstream modern applications. Pressing the middle mouse button transfers the application into the auto-scrolling mode. In this mode, moving the mouse down starts scrolling down the contents even when the mouse is no longer moving. The scroll speed depends on the distance between the current mouse location and the mouse press location. This works for all four directions (up, down, left, right).

There are two existing Swing implementations that i’m aware of:

Both approaches have their drawbacks. The first one effectively changes the UI hierarchy and the second one uses a global frame resource, effectively resulting in repainting the entire UI on every scroll (even when the scroll pane is a small part of the application).

Christopher Deckers has already appeared as a guest author on this blog, and he was very helpful during the development cycles of last two versions of Substance look-and-feel. He has found numerous bugs (many thanks) and made numerous suggestions for enhancing existing features and adding new ones (many thanks). One of his “most wanted” features was the support for auto-scrolling, and after a few iterations he has contributed an elegant implementation that does not use a global resource and does not change the UI hierarchy.

The new LafWidget.AUTO_SCROLL client property can be installed on a specific scroll pane or globally on the UIManager. Once it is set to Boolean.TRUE, the corresponding scroll pane(s) will have the auto-scroll mode installed on them. There are two operation modes:

  • Press the middle mouse button and release it. The scroll pane is transferred into the auto-scroll mode. It can be dismissed by clicking the mouse or scrolling the mouse wheel.
  • Press the middle mouse button and start dragging. The auto-scroll is dismissed by releasing the middle mouse button.

The implementation itself installs a mouse listener on the scroll pane, and when the middle mouse button is clicked, it shows the auto-scroll icon wrapped in a popup menu (thus no need for glass pane). In addition to showing the menu, it also installs a global AWTListener that listens to mouse wheel and mouse move events, scrolling the pane as necessary.

This is the last new feature that was added to the version 4.3 of Substance. It is now in feature freeze state. The release candidate is scheduled on March 31st, with the final release scheduled on April 14.