How do you hide a Swing Popup when you click somewhere else

前端 未结 6 2270
走了就别回头了
走了就别回头了 2021-01-04 06:26

I have a Popup that is shown when a user clicks on a button. I would like to hide the popup when any of the following events occur:

  1. The user clicks somewhere e
6条回答
  •  温柔的废话
    2021-01-04 06:58

    You could add a FocusListener to your popup-window, and dispose it when it loses focus. However, that will cause you some troubles when the focus loss is due to some other application (new windows comes to the foreground, you switch virtual desktops, etc.)

    But perhaps you (a) know that that cannot happen in your case or (b) would want to close the popup in such cases anyway, a focus-based approach may still be interesting to you.

提交回复
热议问题