How can I create or assign a method to temp (WindowAdapter)?

Posted by Doug Hauf on Stack Overflow See other posts from Stack Overflow or by Doug Hauf
Published on 2014-06-04T21:11:02Z Indexed on 2014/06/04 21:24 UTC
Read the original article Hit count: 203

Filed under:

I want to create an instance of the WindowAdapter and put my method for windowClosing in it and then sent the temp into the f.addWindowListener(temp) can this be done.

Java will not let me create an instance of WindowAdapter like below.

 WindowAdapter temp = new WindowAdapter(); <-- Does not compile

How could this be done?

Code:

public static void main(String args[]) {
    setLookFeel();

    JFrame f = new JFrame("Hello World Printer...");

    WindowAdapter temp;


    f.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JButton printButton = new JButton("Print Hello World");
    printButton.addActionListener(new HelloWorldPrinter());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
}

© Stack Overflow or respective owner

Related posts about java