• Post Reply Bookmark Topic Watch Topic
  • New Topic

addWindowListener()/ windowClosing() methods

 
aza chung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use the non-static method addWindowListener() inside my main method and reference a non-static object to it, then it works fine:
public static void main(String[] args) {
JFrame fr = new JFrame();
fr.addWindowListener(...windowClosing(WindowEvent e){
println("Exiting"); //This works
exit(0);
}
}
But when I try to put the same thing in another class under its constructor, and not attaching an object to it, then nothing under windowClosing() works:
public JDialog(){
addWindowListener(...windowClosing(WindowEvent e){
println("Exiting"); //Doesn't work
exit(0);
}
}
I can't put this under JDialog's main method because addWindowListener() is non-static. I also can't create an object to refer addWindowListener() to because a JDialog object has already been created by my JFrame class:
JDialog dialog = new JDialog();
Which is why I placed the addWindowListener() under JDialog's constructor.
Basically, I'm trying to make it so that I can run methods when each dialog/frame closes in my application. Can anyone help me?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make it so that I can run methods when each dialog/frame closes in my application. Can anyone help me?
Perhaps this is what you're trying to figure out:
 
aza chung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dirk for replying. Your sample code made me realize that I should put addWindowListener() BEFORE show()/setVisible(). Now it works fine. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!