views:

299

answers:

2

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.

+3  A: 

Override getToolTipLocation in your cell renderer.

Jonathan Feinberg
Ahh, Sweet!!! thanks. I knew there had to be a simple way, it was right in front of me the whole time.
reccles
I actually made it part of the component and not the cell renderer. The behavior seems more appropriate there compared to the cell.
reccles
I'm glad it worked!
Jonathan Feinberg
A: 

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() ).

Lukasz Czerwinski
Check to make sure the component is registered with the tooltip provider: ToolTipManager.sharedInstance().registerComponent(component);
reccles