This discussion is archived
5 Replies Latest reply: Mar 7, 2008 10:22 AM by 796365 RSS

How to close stream on closing window ?

807601 Newbie
Currently Being Moderated
hi
in case the user closed the client window before signing out how we can make window adapter able to close the stream?
i tried but i failed because main method won't accaept non-static variable.
any suggestion? any alternative?
  • 1. Re: How to close stream on closing window ?
    mlk Newbie
    Currently Being Moderated
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
    http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html
  • 2. Re: How to close stream on closing window ?
    807601 Newbie
    Currently Being Moderated
    hi
    how can use it to close the stream?
    and can i use it to send exit word to server?
  • 3. Re: How to close stream on closing window ?
    796365 Newbie
    Currently Being Moderated
    Post the code that is causing your questions, so that specific issues can be addressed.
  • 4. Re: How to close stream on closing window ?
    807601 Newbie
    Currently Being Moderated
    hi
    here is the method that i want to execute on closing window:
    public void closeStreams() throws IOException{
            
                writer.println("stop");
                in.close();
                out.close();
                bfr.close();
                writer.close();
                s.close();
            
        }
    and here is the main method:
    public static void main(String args[]){
            LoginPage l=new LoginPage();
            l.setTitle("LOGIN ");
            WindowListener listener = new WindowAdapter() {
                public void windowClosing(WindowEvent w) {
                  // do something to close streams
                    System.exit(0);
                }
            };
            l.addWindowListener(listener);
            l.pack();
            l.setVisible(true);
        }
    Edited by: scrolldown on Mar 6, 2008 11:23 PM
  • 5. Re: How to close stream on closing window ?
    796365 Newbie
    Currently Being Moderated
    Create a thread and have it do the tasks in closeStreams

    See some of these hits for example code:
    http://www.google.com/search?q=java+shutdown+hook