0

I'm currently running this in a class that extends a JFrame. When I close the window, I don't see RAN EVENT HANDLER in the console. This is not the main window, and more than one instance 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 method called initialiseEventHandlers() which is called in the constructor, so I'm sure the code is running.

What am I doing wrong?

Thank you!

EDIT: Here's the full (summarised) 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);
}
}
Fares K. A.
  • 1,273
  • 5
  • 24
  • 47

4 Answers4

1

I found out I was using the wrong method: windowClosed(). I should use windowClosing()!

Fares K. A.
  • 1,273
  • 5
  • 24
  • 47
0

This should work

this.addWindowListener(new WindowListener() {
    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("RAN EVENT HANDLER");
    }
});
rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • I'm afraid not. Already tried that. Defining a WindowListener object means I have to write up all the unimplemented methods and leave them empty. I only want the windowClosed method. – Fares K. A. Mar 05 '14 at 10:14
  • yeah.. you have to implement all the methods in the Interface. But using WindowListener interface triggered the event for you? – rogue-one Mar 05 '14 at 10:19
  • No, it hasn't. Even if I did implement all the methods as empty methods, it still doesn't trigger it. By windowClosed, they mean pressing the x on the corner of the window, right? – Fares K. A. Mar 05 '14 at 10:25
  • Yes. I tried and it is working for me. the this in this.addWindowListener , is it a JFrame class or a subclass of it? – rogue-one Mar 05 '14 at 10:26
  • No, it's a subclass of JFrame (i.e. className extends JFrame). – Fares K. A. Mar 05 '14 at 10:33
  • Can you post the entire code.. to spot issues like the code being unreachable etc. – rogue-one Mar 05 '14 at 10:34
  • Done! :) Please check my original post! – Fares K. A. Mar 05 '14 at 10:42
0

Add this to your constructor.

setDefaultCloseOperation(EXIT_ON_CLOSE);

rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • I don't want the whole program to quit or terminate.. it's a program with several windows. When closing a certain Window I want it to log that. – Fares K. A. Mar 05 '14 at 10:46
  • then please try setting this setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); – rogue-one Mar 05 '14 at 10:51
  • It disables the x button on the corner from working. I've figured out the answer. I was using the wrong method. Thank you so much for your help! – Fares K. A. Mar 05 '14 at 10:53
0

The code below 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
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Goz E.
  • 81
  • 1
  • 5
  • @Donald You edited it but the answer was pretty much "I confirm that this [answer](http://stackoverflow.com/a/22195661/5292302) works, here is my code", check revision for more details... but oki doki – Petter Friberg Feb 18 '17 at 20:16