如何控制JComponent的Tooltip的显示?

时间:2022-08-26 23:35:06

I have a JComponent that's painting various shapes on itself. I'm detecting whenever the mouse enters one of these shapes and changing the tooltip accordingly.

我有一个JComponent,它自己绘制各种形状。我正在检测鼠标何时进入其中一个形状并相应地更改工具提示。

The problems I'm having are:

我遇到的问题是:

  • The tooltip doesn't follow the mouse as the user tracks the mouse across the shape. It stays where it was first set and then only jumps whenever another shape changes the tooltip.
  • 当用户在形状上跟踪鼠标时,工具提示不会跟随鼠标。它保持在第一次设置的位置,然后只有在另一个形状改变工具提示时才会跳转。

  • It takes about a second for the tooltip to appear, but I'd like it to appear immediately.
  • 工具提示显示需要大约一秒钟,但我希望它能立即显示。

Can someone suggest a way of getting these behaviours without writing a custom tooltip mechanism?

有人可以建议一种获得这些行为而无需编写自定义工具提示机制的方法吗?

2 个解决方案

#1


6  

Take a look at the ToolTipManager.

看一下ToolTipManager。

You can register your component with that manager and then adjust a number of settings. Its pretty straight forward to use.

您可以使用该管理器注册组件,然后调整许多设置。它非常直接使用。

That at least can solve your initialdelay problem.

这至少可以解决你的初始延迟问题。

For your first problem you can overide the createTooltip command from your component to get a hold of the JTooltip instance. and then its easy make the position change whenever you move your mouse(aka follow your mouse) as its a subclass of the JComponent class.

对于您的第一个问题,您可以从组件中覆盖createTooltip命令以获取JTooltip实例。每当你移动鼠标(也就是你的鼠标)作为JComponent类的子类时,它就会轻松改变位置。

#2


1  

To solve your first issue of where the tooltip doesn't follow the mouse, if you override the getToolTipLocation(MouseEvent e) in JComponent, you can return the point for where you want to the display the tooltip. The MouseEvent will allow you to retrieve the x and y.

要解决第一个工具提示不遵循鼠标的问题,如果在JComponent中覆盖getToolTipLocation(MouseEvent e),则可以返回要显示工具提示的位置。 MouseEvent将允许您检索x和y。

#1


6  

Take a look at the ToolTipManager.

看一下ToolTipManager。

You can register your component with that manager and then adjust a number of settings. Its pretty straight forward to use.

您可以使用该管理器注册组件,然后调整许多设置。它非常直接使用。

That at least can solve your initialdelay problem.

这至少可以解决你的初始延迟问题。

For your first problem you can overide the createTooltip command from your component to get a hold of the JTooltip instance. and then its easy make the position change whenever you move your mouse(aka follow your mouse) as its a subclass of the JComponent class.

对于您的第一个问题,您可以从组件中覆盖createTooltip命令以获取JTooltip实例。每当你移动鼠标(也就是你的鼠标)作为JComponent类的子类时,它就会轻松改变位置。

#2


1  

To solve your first issue of where the tooltip doesn't follow the mouse, if you override the getToolTipLocation(MouseEvent e) in JComponent, you can return the point for where you want to the display the tooltip. The MouseEvent will allow you to retrieve the x and y.

要解决第一个工具提示不遵循鼠标的问题,如果在JComponent中覆盖getToolTipLocation(MouseEvent e),则可以返回要显示工具提示的位置。 MouseEvent将允许您检索x和y。