Forums Register Login

save on close problem?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1268 times.
Similar Threads
save object?
Reading File into an array
runtime exception
Two separate classes (not inner classes) and ActionListener question
OverStack?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:09:13.