views:

245

answers:

2

In a Java program, I am using a custom renderer for cells in a JTable. On this renderer I set a tooltip, for which the content depends on the current cell.

When the values are different, the tooltip is updated, and will appear next to the mouse pointer, over the cell.

However, when the text for this tooltip is identical when changing cell (it happens that a few cells have the same text for tooltip), the TooltipManager considers that the tooltip hasn't changed, and it leaves the previous one, on the previous position.

Does someone knows how to make it so that the tooltip would be updated on each cell, even with identical values?

+1  A: 

Add or remove a zero-width, non-breaking space?</hack>

Tom Hawtin - tackline
A bit hacky indeed, and this would need to add a different number of such spaces, depending on the cell. Though I guess that with a "odd/even on row+column" rule, it would work (making a checkers grid). I will check into that, if nothing else.
Gnoupi
+3  A: 

I think your best bet is to override getToolTipLocation(MouseEvent) in your component, and have it track the location of the mouse. If either the text or the location of a tooltip have changed, then the tooltip will update.

Jonathan Feinberg
Indeed, that would be a good solution. Checking the sources, it appears that the JTable doesn't extend this method, letting Swing decide where to put the tooltip. Thanks to that, only the text determines if a tooltip should be changed or not. To apply this method, I would have to extend the JTable, though (and refactor a few things), so that it would delegate this call to the renderer (or simply return the current position all the time).
Gnoupi