Adding a new WindowListener to a JFrame

mainFrame.addWindowListener(new WindowListener() { @Override public void windowClosing(WindowEvent e) { if (JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to quit?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) { return; } System.exit(-1); } @Override public void windowOpened(WindowEvent e) {} @Override public void windowClosed(WindowEvent e) {} @Override public void windowIconified(WindowEvent e) {} @Override public void windowDeiconified(WindowEvent e) {} @Override public void windowActivated(WindowEvent e) {} @Override public void windowDeactivated(WindowEvent e) {} }); 

There is my code, is this possible, since I use only the windowClosing method to remove everything else in my case, useless methods, so it takes up less space?

Example

  mainFrame.addWindowListener(new WindowListener() { @Override public void windowClosing(WindowEvent e) { if (JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to quit?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) { return; } System.exit(-1); } }); 

Is it possible?

+4
source share
1 answer

There is a default WindowAdapter implementation of the WindowListener that allows you to override the methods you really want to use

+10
source

Source: https://habr.com/ru/post/1443798/


All Articles