Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

ToolTips and ScrollPanes

Posted by Rob Camick on November 8, 2009

Tooltips are generated by mouse movement over different Swing components. In a complex component, like a JTable, the tooltip is continually updated as the mouse moves from cell to cell. But what happens when the table is in a scrollpane and the viewport is moved? In this case the mouse may now be hovering over a different cell, but the tooltip is not updated. This may, or may not, be a problem depending on your requirements.

First you might ask why would the viewport move? Well, in the simplest case you would be using the mouse wheel to scroll through the rows of the table. Or you could be using the keyboard to scroll up/down/left/right in the table. In these cases it would be nice to see the tool tip for the cell the mouse is positioned over after the scrolling! The problem is that the ToolTipManager is not aware of the relative change of mouse position within the table because no mouseMoved events have been passed to it (since in reality the mouse hasn’t actually moved). A simple solution is to manually generate mouseMoved events as the viewport is scrolled.

The ToolTipListener class can be used to generate these mouseMoved events. A ToolTipListener is actually 3 listeners in 1 and can be used to generate the mouseMoved events in different situations depending on your requirements. When the ToolTipListener is used as a:

  • MouseWheelListener – it is added to the scrollpane. In this case the mouseMoved events are only generated by scrolling of the mouse wheel and therefore only supports vertical movement of the viewport.

    scrollPane.addMouseWheelListener(new ToolTipListener());

  • AdjustmentListener – it is added to the vertical and/or horizontal scrollbar of the scrollpane. In this case the viewport can be scrolled by using the mouse wheel or the keyboard and mouseMoved events will be generated.

        .addAdjustmentListener(new ToolTipListener());

  • ComponentListener – it is added to the component. In this case all forms of viewport movement as well as changes in the component size will cause the mouseMoved event to be generated.

    table.addComponentListener(new ToolTipListener());

Although this discussion has revolved around using a table in a scroll pane,
the ToolTipListener class can be used on any Swing component added to a scroll pane.


This entry was inspired by this posting from the Sun forum and incorporates some of the ideas presented by Maxideon, Jeanette and myself in the discussion of the solution.

Try The Demo

Launch – Using Java™ Web Start (JRE 6 required)

Get The Code

Related Reading

Java Tutorials: How to Use Tool Tips


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: