Extending Swing’s ToolTipManager to change behaviour on hover?

I’d like to implement a ToolTip in Swing that has customised behaviour: the longer the user hovers over the component, the more detail should be shown in the tooltip (i.e., a few new lines are added after a few seconds of the user hovering over the component). I just need to check whether this is really doable with Swing without things getting too messy. My idea at the moment would probably be:

  • Extend ToolTipManager
  • Override mouseEntered to start a timer (maybe use javax.swing.Timer?). Call setToolTipText and createToolTip to refresh the tooltip and add new information at regular intervals
  • Override mouseExited to reset the timer
  • Probably use setDismissDelay to set the dismiss delay to something a lot longer (or Integer.MAX_VALUE)

Is such a thing feasible or is this not a good way to work with Swing (sorry, I’m pretty new to it)? Is there a better way of doing this?


Source: java

Leave a Reply