Run some code when a popup closes in my swing app

依然范特西╮ 提交于 2020-07-09 17:12:23

问题


In my app I display a popup using JPopupMenu. I want to run some code when this popup closes (either directly, programmatically or when escape key is pressed). For windows I can attach a WindowListener but JPopupMenu doesn't have any corresponding feature, and SwingUtilities.windowForComponent returns the root window of the app. How do I implement this?


回答1:


How about adding a PopupMenuListener to it? Something like:

jpopMenu.addPopupMenuListener(new PopupMenuListener 
{
  public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) 
  {
    //here the code you want to be executed at close
  }
  public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent){}
  public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {}
}

This should be automatically executed when you cancel/close the popMenu. I didn't add code to the other two methods, but feel free to play with them if needed.



来源:https://stackoverflow.com/questions/62765704/run-some-code-when-a-popup-closes-in-my-swing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!