Java listener on dialog close

女生的网名这么多〃 提交于 2019-11-29 11:12:26

问题


I have a Java app that displays a list from a database. Inside the class is the following code to open a new dialog for data entry:

@Action
public void addNewEntry() {
    JFrame mainFrame = ADLog2App.getApplication().getMainFrame();
    addNewDialog = new AddNewView(mainFrame, true);
    addNewDialog.setLocationRelativeTo(mainFrame);
    addNewDialog.addContainerListener(null);
    ADLog2App.getApplication().show(addNewDialog);
}

How do you add a listener to the main class to detect when the addNewDialog window is closed, so that I can call a refresh method and refresh the list from the database.


回答1:


If AddNewView is a Window such as a Dialog or JDialog, you could use the Window.addWindowListener(...). That is, in your main class, you do

addNewDialog.addWindowListener(someWindowListener);

where someWindowListener is some WindowListener (for instance a WindowAdapter) which overrides / implemetnns windowClosed.

A more complete example, using an anonymous class, could look like

addNewDialog.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        refreshMainView();
    }
});

Relevant links:

  • Official Tutorial: How to Write Window Listeners



回答2:


you have to add WindowListener and override windowClosing Event, if event occured then just returs some flag, example here



来源:https://stackoverflow.com/questions/7652821/java-listener-on-dialog-close

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