I use JComponent.setToolTipText to get tooltips in my java application, but unlike, e.g., e开发者_StackOverflow中文版clipse, if I switch to another application when a tip is showing, it remains hovering over what I'm doing until I switch back to the java app and move the mouse around to get it off the element before returning to the alternate application. Is there a way to make the tooltips in my app better citizens? to make them disappear when the app loses focus?
I'm on a mac if relevant.
I've found a solution! (with some input from a colleague)
<myJFrame>.addWindowFocusListener(new WindowFocusListener() {
@Override @SuppressWarnings("unused")
public void windowLostFocus(WindowEvent e) {
ToolTipManager.sharedInstance().setEnabled(false);
}
@Override @SuppressWarnings("unused")
public void windowGainedFocus(WindowEvent e) {
ToolTipManager.sharedInstance().setEnabled(true);
}
});
A WindowAdapter listening to windowActivated/Deactivated also works. A plain FocusListener does not.
精彩评论