void (final Runnable onWindowClosing, final Window window) { if (onWindowClosing != null) { window.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { onWindowClosing.run(); window.removeWindowListener(this); } }); } }