Java: window WindowAdapterClosed method not working
I am currently running this on a class that extends the JFrame. When I close the window, I cannot see RAN EVENT HANDLER
in the console. This is not the main window, and multiple instances of this window can exist at the same time.
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("RAN EVENT HANDLER");
}
});
This method is inside a named method initialiseEventHandlers()
that is called in the constructor, so I'm pretty sure the code is running.
What am I doing wrong?
Thank!
EDIT: Here's the complete (generic) code:
public class RacesWindow extends JFrame {
private JPanel mainPanel;
private JLabel lblRaceName;
private JTable races;
private DefaultTableModel racesModel;
public RacesWindow() {
this.lblRaceName = new JLabel("<html><strong>Race: " + race.toString()
+ "</strong></html>");
initialiseComponents();
this.setMinimumSize(new Dimension(500, 300));
this.setMaximumSize(new Dimension(500, 300));
initialiseEventHandlers();
formatWindow();
pack();
setVisible(true);
}
public void initialiseComponents() {
mainPanel = new JPanel(new BorderLayout());
races = new JTable();
racesModel = new DefaultTableModel();
races.setModel(racesModel);
}
public void initialiseEventHandlers() {
System.out.println("EVENTHANDLER CODE IS CALLED"); //for debugging
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("RAN EVENT HANDLER");
appManager.removeOpenWindow(race.toString());
}
});
}}
public void formatWindow() {
mainPanel.add(lblRaceName, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(races), BorderLayout.CENTER);
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
this.add(mainPanel);
}
}
0
source to share
4 answers
The following code worked for me.
// parent class {
// constructor {
...
this.addWindowListener(new GUIFrameListener());
...
}
class GUIFrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Window Closed");
}
}
} // end of parent class
0
source to share