• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

addWindowListener()/ windowClosing() methods  RSS feed

 
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?
 
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!