I have JComponent with a custom createToolTip() however the GlassedPane does not call the underlying components createTooltip() directly so my tooltips aren’t being displayed properly. Here is an example that fails when placed in a GlassedPane:
new JPanel(){
@Override
public String getToolTipText(MouseEvent event){
if(event.getPoint().x < 200){
return "Test";
}else{
return "Test2";
}
}
@Override
public JToolTip createToolTip(){
JToolTip tip = new JToolTip();
tip.setBackground(Color.RED);
return tip;
}
}
My solution is to change GlassedPane to call the underlying components createToolTip, where it is of type JComponent. However I was wondering if this problem has been encountered before and if there is an alternate solution?
You are the first one overriding “createToolTip”, or all the other people were too afraid to ask…
Yeah, the GlassedPane just copies the text, but uses its own tooltip - I see the issue.
“createToolTip” is always called if the mouse hovers over a new Component, but because the GlassedPane catches all MouseEvents “createToolTip” of the GlassedPane is called.
It would certainly be possible to call “createToolTip” of the underlying component the first time a tooltip shows up. Additionally the GlassedPane would need to hide a tooltip if the mouse moves over another component, this way a new call to “createToolTip” would be triggered. The API to close a tooltip is not public, but looking at the code of ToolTipManager is see some non-official ways to call “setEnabled” in order to trigger the hide-method anyway.
I’ll try and get this to work, may take a few days… I’ll write again either if I found a solution or if I’m stuck.
Alright, I added the feature, but I am not yet satisfied with its performance. Sometimes the tooltip just does not appear… I’ll try to find a fix for that. In any case, the feature will be included in 1.1.1p6d, and I’ll upload that version next weekend.
If you have a better idea on how to implement the tooltip… there is an interface “TooltipStrategy” allowing you to write your own code. It can be installed using the property key “GlassedPane.TOOLTIP_STRATEGY”. The updated files are now online.