How to show hint text in Java Graphics2D
I am wondering if it is possible to show a "hint text" on mouseover in Java Graphics2d. something like:
Graphics2D g;
g.fillRect(0, 0, w, h);
g.showhint("Show this hint if开发者_开发问答 mouse is over the area:",0,0,10,20); ?????
Thanks.
If you're referring to some arbitrary painted text, then sure:
- Add a mouse-listener that records when the curser enters or leaves the desired area.
- Let the listener set a boolean variable according to the events
- Whenever the boolean changes, call
repaint()
- Let the paint-method paint the hint if the boolean is set.
If you're referring to tooltips: Yes, it is possible too.
Here is an excerpt from the official tutorial:
Even in components that have no API for setting part-specific tool tip text, you can generally do the job yourself. If the component supports renderers, then you can set the tool tip text on a custom renderer. The table and tree sections provide examples of tool tip text determined by a custom renderer. An alternative that works for all
JComponents
is creating a subclass of the component and overriding itsgetToolTipText(MouseEvent)
method.
The latter approach seems even trivial to implement.
精彩评论