Java listener on dialog close

40,009

Solution 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:

Solution 2

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

Share:
40,009
Woodsy
Author by

Woodsy

Developer doing research and development with web and android projects. Background in software engineering and computer science. Enjoys brewing beer.

Updated on September 20, 2020

Comments

  • Woodsy
    Woodsy over 3 years

    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.

  • Andrew Thompson
    Andrew Thompson over 12 years
    Great links. In regard to the ones to the JavaDocs. Until such times as bug report 7090875 (an RFE) is resolved, any chance of you throwing a '7' into any search for the doc for a class (or otherwise linking to the version 7 docs)?
  • aioobe
    aioobe over 12 years
    Yeah. Perhaps it's time to start referring to v7... Hadn't struck my mind. I'll do it from now on. Thanks.
  • Andrew Thompson
    Andrew Thompson over 12 years
    Thanks back. The sooner that people start linking to the v. 7 docs, the sooner Google will return those links over the v. 6 links. Of course, if the RFE is implemented, it will all become moot.
  • corochann
    corochann over 7 years
    Just to add note that windowClosed is called when dispose() method are used, and windowClosing is called when user press 'X' button on top-right of the JDialog.