Results 1 to 4 of 4
  1. #1
    benn22 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default Write to File After Closing

    As part of an assignment, I have to write to a file after my JFrame component closes. Right now, it will write to a file regardless of its visibility. Here is my write method:

    Java Code:
    	public static void write(String text){
    		FileWriter fw = null;
    		PrintWriter pw = null;
    		try{
    			fw = new FileWriter("updated_contacts.txt", true);
    			pw = new PrintWriter(fw);
    			pw.println(text);	
    			pw.close();
    		}catch (IOException e){
    			JOptionPane.showMessageDialog(null, "File 'updated_contacts.txt' not found.", "Error", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    I also have an actionPerformed method, but this simply calls the method above with a string as its parameter. Nonetheless, I will provide the code if requested.

    Thanks.
    Ben

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Java Code:
    jframe.addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent e) {
            //code when jframe closes
        }
    });

  3. #3
    benn22 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    Solution:

    Java Code:
    public class MyFrame extends JFrame{ //class
            ...
    	public MyFrame(){ //constructor
    		listenToWindow(); //call method
                    ...
    	public void listenToWindow(){ //method
    		addWindowListener //execute my window listener
    		  (new WindowAdapter(){ //used WindowAdapter
    			public void windowClosing(WindowEvent e){ //an inherited method from class WindowListener
    			  setTitle("Closing window..."); //change current title to identify that it is closing
    			  try{
    				 Thread.sleep(SLEEP);
    			  }catch (InterruptedException error){
    				 System.out.println(error.getMessage());
    			  }
    			  for (int i=0; i<list.getModel().getSize(); i++) //get size of list
    			    ioStream.write((String)list.getModel().getElementAt(i)); //write current size of list to updated_contacts.txt
    			  setVisible(false); //frame is no longer viewable
    			  dispose(); //eliminate frame
    			  System.exit(0); //dispose process
    			}
    		  });
    	}
    Much thanks ra4king.

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  3. Write to a file in a jar
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 06-12-2009, 09:27 PM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •