开发者

Making java tooltips disappear with application switch

开发者 https://www.devze.com 2023-01-14 00:05 出处:网络
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 hoveri

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消