问题
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