fancy tooltips in Swing
I have a JTable that I would like to display a fancy tooltip (basically a JTextArea) for particular cells in a column. I am using a custom cell renderer, so it would be easy if I could f开发者_开发百科igure out how to popup a window when I hover over the cell renderer's component.
Are there any examples of how to do this?
You can use HTML in tooltips, if you use the <html>
and </html>
tags around the content.
Use HTML to format the tooltip. Using colored (<font>
) and multi-line (<br>
) tooltips is now easy.
Creating and overwriting the default JToolTip
is a bit harder. Every component has a JToolTip
instance and you can retrieve this one with JComponent.createToolTip()
. To create a custom tooltip, extend the cell renderer and override it's createToolTip
to implement your custom functionality (return a custom extended version of JToolTip
).
I'm not sure I totally am clear on what sort of customizations specifically you're hoping to do, so I'll be general here.
There is a class, UIManager, that controls the look and feel of components, including the swing ToolTip class. The simple way to make an easy change is to call a method in UIManager to set properties for the tooltips. Doing this you could do things like use BorderFactory to add a decorative border, or change the background color, etc.
Here are some examples of changing some of these properties:
UIManager.put("ToolTip.background", new ColorUIResource(255, 247, 200)); // The color is #fff7c8.
Border border = BorderFactory.createLineBorder(new Color(76,79,83)); // The color is #4c4f53.
UIManager.put("ToolTip.border", border);
ToolTipManager.sharedInstance().setDismissDelay(15000);// 15 seconds
If you want to change a whole bunch of things about their look and feel, it is better to extend your current look and feel with a class implementing custom tooltip look and feel. A full example of this can be found in this blog post by a former Sun developer.
You might also have a look at this Stack Overflow question on how to set the insets on a tooltip.
精彩评论