Search code examples
javaswingwindowlistener

Implementing WindowListener Error


Hello I'm having a problem with adding a WindowListener to my JFrame... It's saying "windowClosing can't be resolved to a type" and I don't know how to fix the error.

public Editor() {
    //Create JFrame For Editor
    JFrame SimplyHTMLJFrame = new JFrame();

    SimplyHTMLJFrame.setTitle("Simply HTML - Editor");
    SimplyHTMLJFrame.setSize(800, 600);
    SimplyHTMLJFrame.setResizable(true);
    SimplyHTMLJFrame.setLocationRelativeTo(null);
    SimplyHTMLJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    SimplyHTMLJFrame.addWindowListener(new windowClosing()); //The error is here it underlines windowClosing in red
    SimplyHTMLJFrame.setVisible(true);
    System.out.println("Editor - JFrame 'SimplyHTMLJFrame' - Created");

    //Program Closing Alert
    public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n"
                    + "All unsaved changes will be lost!","Confirm", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
        if (result == JOptionPane.YES_OPTION) {
            System.exit(0);
        } else {
            //Do nothing
        }
    }
}

Solution

  • You have to implement an inner class for the WindowListener callback.

    public class Editor {
    
      public Editor() {
    
        // Create JFrame For Editor
        JFrame SimplyHTMLJFrame = new JFrame();
    
        SimplyHTMLJFrame.setTitle("Simply HTML - Editor");
        SimplyHTMLJFrame.setSize(800, 600);
        SimplyHTMLJFrame.setResizable(true);
        SimplyHTMLJFrame.setLocationRelativeTo(null);
        SimplyHTMLJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
        SimplyHTMLJFrame.addWindowListener(new WindowAdapter() {
          // Program Closing Alert
          public void windowClosing(WindowEvent e) {
            int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\n" + "All unsaved changes will be lost!", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    
            if (result == JOptionPane.YES_OPTION) {
    
              System.exit(0);
    
            } else {
    
              // Do nothing
    
            }
          }
        }); // The error is here it underlines windowClosing in red
    
        SimplyHTMLJFrame.setVisible(true);
        System.out.println("Editor - JFrame 'SimplyHTMLJFrame' - Created");
    
      }