reccles
reccles

Reputation: 5224

Continuous Tooltip Update in Swing

I have a custom swing component that is implemented similar to a JTree. It has a ComponentUI that renders an object list using a CellRenderer. The tooltip now correctly shows for each rendered cell, however it doesn't track the mouse as I would like. For instance, if I have boxes layed out like this;

 [ box A ]    [ box B ]   [ box C ]   [ box D ]

If i drag the mouse across these boxes I will see the tooltip for box A where the mouse crosses the box A boundary. As I continue to move the mouse the tooltip will not track the pointer. When I leave the box the tooltip will appropriately disappear. When i cross box B the same thing happens.

My guess is that the tooltip is only updating the view when the contents of getToolTipText(MouseEvent event) change. The behavior I would like is to have the tooltip track the pointer position regardless of the contents returned by getToolTipText. A quick hack is to add and remove an empty space based on an even/odd number of calls to the method. This works, but bleh, it can't be the way I am supposed to do it.

I'm looking for a graceful non-hack way of solving this problem. I'm hoping someone knows of some arcane flag somewhere that forces the tooltip to follow the mouse regardless of mouse content.

Upvotes: 3

Views: 2795

Answers (2)

Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15482

I tried to use getToolTipLocation to set tooltip's position when pointing on elements of a JList, but it didn't work.

After inserting breakpoints it came out that in such code:

public class DefinitionListCellRenderer extends JTextArea implements
        ListCellRenderer, ComponentListener {

...

    @Override
    public Point getToolTipLocation(MouseEvent event) {
        Point pt = new Point(event.getX(), event.getY());
        return pt;
    }


...

}

getToolTipLocation isn't even called!! Could you please say, why? DefinitionListCellRenderer is a single item on a list (has getListCellRendererComponent() ).

Upvotes: 0

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45364

Override getToolTipLocation in your cell renderer.

Upvotes: 6

Related Questions