CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009
    Posts
    26

    [RESOLVED] using an anonymous inner class

    how do i use an anonymous inner class with this code:
    Code:
    class MyFrame extends JFrame{
    MyListener listener = new MyListener();
    public MyFrame(){
         addWindowListener(listener);
    }
    }
    
    class MyListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: using an anonymous inner class

    If you want to exit the application when the main window frame closes, the usual way is to set the frame's default close operation to DISPOSE_ON_CLOSE (or for uncontrolled exit, EXIT_ON_CLOSE). See the JFrame API docs for details:
    Code:
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Remember that (as we already told you) you should avoid using System.exit(), especially in GUI applications.

    If you want to add a WindowListener using an anonymous class, it would go something like this:
    Code:
    public MyFrame(){
       addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
             System.out.println("Window is closing");
          }
       });
    }
    The problem is never how to get new, innovative thoughts into your mind, but how to get old ones out!
    D. Hock
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured