[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);
}
}
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