wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Not able to delete file using JButton in a frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Not able to delete file using JButton in a frame " Watch "Not able to delete file using JButton in a frame " New topic
Author

Not able to delete file using JButton in a frame

ahmedali mohd
Greenhorn

Joined: Apr 14, 2010
Posts: 15

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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
ahmedali mohd
Greenhorn

Joined: Apr 14, 2010
Posts: 15

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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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.
 
 
subject: Not able to delete file using JButton in a frame
 
Similar Threads
File renameTo()
AWT Event
File I/O
error while creating a new file
problem in rediredtin GUI to IO Streams