• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

save on close problem?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I wish to save a file on close but it wont allow me to refrence any objects as it is static where I'm trying to set it up. My main class "TabDemo" has implenents actionListener but I can't also put implements WindowListener can I?
Please can you tell me how I might get this WIndowListener to work?
Here is a bit of the code:

The errors it gives me are like this:
C:\TabDemo.java:326: non-static variable this cannot be referenced from a static context
frame.addWindowListener(this);
^
C:\TabDemo.java:326: addWindowListener(java.awt.event.WindowListener) in java.awt.Window cannot be applied to (TabDemo)
frame.addWindowListener(this);
^
2 errors
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you're running inside a static method (such as main), there is no such thing as "this" as the method belongs to the class itself and not to a particular instance of the class.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Berdy Ahern:
My main class "TabDemo" has implenents actionListener but I can't also put implements WindowListener can I?


Yes actually you can. A class can implement multiple interfaces but can only extend one class.

As James mentions above, you cannot use the keyword "this" in a static method since there is no "current object". If I understand what you are doing, you should change

to

This will also mean that TabDemo needs to implement WindowListener which, as I said before, is not a problem even if it already implements ActionListener or any other interface.

HTH

Layne
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic