Java Tips Weblog

  • Blog Stats

    • 1,912,691 hits
  • Categories

  • Archives

Scrollbar Tooltip

Posted by Rob Camick on April 11, 2009

All Swing components support the usage of tooltips. If a tooltip is set for a given component then the tooltip will display when the mouse hovers over the component for a given period of time. Once the tooltip is displayed it may also be changed dynamically as the mouse moves over different areas of the component. Unfortunately this basic processing does not work for a JScrollBar. Typically a scrollbar will be dragged by the mouse, however the dragging action causes any tooltip that might be visible to be hidden (actually the mouse pressed causes the tooltip to be hidden). Therefore, a dynamic tooltip is not possible on a scrollbar.

The ScrollBarToolTip class will provide the necessary support to allow you to simulate dynamic tooltips. You will be able to specify the style, position and text of the tooltip. You can choose from one of the two main styles:

  • Fixed – the tooltip remains fixed once it is displayed
  • Floating – the tooltip moves along with the movement of the scrollbar thumb

There are actually 5 tooltip styles to choose from:

  • Fixed Pressed (default) – the tooltip is fixed at the location where the mouse was pressed to begin dragging
  • Fixed Start – the tooltip is fixed at the start of the scrollbar (top or left, depending on the orientation)
  • Fixed End – the tooltip is fixed at the end of the scrollbar (bottom or right, depending on the orientation)
  • Float Start – the tooltip floats towards the start of the scrollbar as the thumb is dragged
  • Float End – the tooltip floats towards the end of the scrollbar as the thumb is dragged

The position of the tooltip, relative to the scrollbar can also be controlled:

  • Center – the tooltip is centered over the scrollbar
  • Inside (default) – the tooltip is located inside the scrollpane the scrollbar belongs to
  • Inside Edge – the tooltip is justified to the inside edge of the scrollbar
  • Outside – the tooltip is located outside the scrollpane the scrollbar belongs to
  • Outside Edge – the tooltip is justified to the outside edge of the scrollbar

The tooltip text is controlled by the getToolTipText(…) method. The default implementation uses the current postion of the scrollbar thumb to calculate the percentage of this value in relation to the maximum value. It is strongly suggested that you would override this method to provide a more meaningfull implementation based on your knowledge of the component being displayed in the scrollpane.

The image below shows what the tooltip would look like using the default settings (assume I just pressed the left mouse button while positioned on the scrollbar thumb):

scrollbar-tooltip

With 5 different styles and 5 different locations you have 25 different possibilities when choosing your desired tooltip behaviour. I know I got a bit carried away here and that many combinations don’t make sense. So, I have included my little test program that allows you to experiment with the different combinations to help you determine which ones you might use. The test program also shows a simple example of customizing the tooltip text for the horizontal scrollbar.

Get The Code

ScrollBarToolTip.java
ScrollBarToolTipTest.java

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: