• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to delete file using JButton in a frame

 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to delete a file when i click the JButton or unchecked the JCheckBox option.. I m added actions to these components a well. Doing action but not deleting the file Why i don't know.
See the code ... I tried on deleteonExit() as well. But it is not doing if im doing through main() method that normal class, i can delete it ...

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
update_user();
// delete_operation();
//System.exit(0);
}

public void windowClosed(WindowEvent e) {

File f1=new File("ahmedali.dat");
System.out.println("You are in Foperations"+f1);
System.out.println(" Inside delete");
if(f1.exists()) { System.out.println("File is deleted:");f1.delete(); }
f1=null;
}

}

);
 
Maneesh Godbole
Saloon Keeper
Posts: 11164
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using an ActionListener to perform the delete operation when the button is clicked.
Recommended reading http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
 
ahmedali mohd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I ve performed action listeners to button already and i 've used mouselisteners also to do the operations, but it is not working... please let me know .
 
Maneesh Godbole
Saloon Keeper
Posts: 11164
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you have noticed, you have posted some WindowAdapter code which tries to delete the files.
If you need help, you need to help us help you by posting SSCCE code which we can try out and identify the problem.

PS. Please do take out time to read what SSCCE really means before posting code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic