So I have a PrintWriter as a class variable which is created upon initialization. I want to make sure it gets closed when the Applet closes, but overriding the destroy() method doesn't seem to work (a System.out.println inside the method does not appear).

  1. should I be doing this in the first place, or should I open and close the file each time I need to write to it?

  2. what can I do to make sure the PrintWriter gets closed?

  3. does the PrintWriter even need to be closed?

Thanks in advance

1 depends on how often that will happen, but probably no.
2 how does yopur app terminate? - do you have a method? What event triggers termination? do you call System.exit? Do you have more than one thread?
3 you may be able to get away with that, but it's not recommended.

It seems odd that your destroy() is not being called. Have you tried stop() as well? Do you have an @Override annotation on the overridden methods? - this will pick up any situations where you have made an error in the method signature and thus failed to override properly.

Edited 3 Years Ago by JamesCherrill

Thanks for the quick reply!

  1. I write to it very frequently (each time the user presses a button), so opening/closing all the time seems rather ineffectve
  2. I'm running it off of Eclipse, and I'm clicking the X button at the top right

I tried your suggestions to no avail :/

I suspect the Eclipse applet runner is just hard terminating the app when you click x?. Try running it as a normal applet in a browser... if your @Override is not giving an error then destroy should definitely be called when the applet terminates.
Your best strategy may be to open the stream in the start() method and close it in stop(), thus not leaving it open when the user is doing something else in his browser.

Edited 3 Years Ago by JamesCherrill

Update: I just tried a trvial example in Eclipse, and the destroy method is called when you close the applet viewer via its red X. Here's the code I used

import javax.swing.*;

public class AppletTest extends JApplet {

   @Override
   public void init() {
      add(new JLabel("Hello World"));
    }

    @Override
    public void destroy() {
       System.out.println("Destroyed");
    }

}

So the question now is: what are you doing in your situation which stops it working relative to that simplest case?

Edited 3 Years Ago by JamesCherrill

It turns out that an old issue came back to haunt me...

Since I wanted to export my applet as a runnable jar, I had to make a main method to instantiate the applet. I didn't know what to do so I just created a JFrame and added the applet inside. I think that when I press the X, I'm exiting the JFrame, not the applet.....

Now the problem is, how can I make my applet runnable without losing the ability to close my streams?

Instead fo taking the standrad "exit on close" option for your JFrame, listen for the close event and use that to call the moethods to shut down your app properly

   addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
              etc etc
This question has already been answered. Start a new discussion instead.