Determination of opening or closing events of frames can be done by adding an instance of java.awt.event.WindowListener interface to that frame.

// Create the frameFrame frame = new Frame();// Create a listener for window eventsWindowListener listener = new WindowAdapter() {// This method is called after a window has been openedpublic void windowOpened(WindowEvent evt) {}// This method is called when the user clicks the close buttonpublic void windowClosing(WindowEvent evt) {}// This method is called after a window is closedpublic void windowClosed(WindowEvent evt) {}};// Register the listener with the frameframe.addWindowListener(listener);