Friday, September 24, 2010

DeletingTool

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.BorderFactory;
class DeletingTool implements ActionListener
{
JFrame f;
JTextField tf;
JButton b1,b2,browse;
JLabel label,lb;
DeletingTool()
{
String labeltext="Enter the PATH of Folder/File to DELETE........."+"Eg.. f:/games to delete games folder in Drive f"+
"& f:/milan.txt to delete milan.txt in Drive f";
f=new JFrame("Deleting Tool");
tf=new JTextField("");
b1=new JButton("Delete");
b2=new JButton("Clear");
browse=new JButton("Browse");
lb=new JLabel(" Created By :- > Milan");
label=new JLabel(labeltext,JLabel.CENTER);
label.setBorder(BorderFactory.createTitledBorder("How To Do"));
f.setLayout(null);
tf.setBounds(40,40,265,40);
b1.setBounds(40,110,100,50);
b2.setBounds(200,110,100,50);
label.setBounds(40,170,265,80);
lb.setBounds(40,5,265,40);
browse.setBounds(125,260,80,40);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf);
f.add(b1);
f.add(b2);
f.add(label);
f.add(lb);
f.add(browse);

f.setSize(350,350);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
browse.addActionListener(this);
f.setLocation(400,200);
}

public static void main(String... s)
{
new DeletingTool();
}

public void actionPerformed(ActionEvent e)
{
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(e.getActionCommand()=="Delete")
{
String path=tf.getText();
File f=new File(path);
if(!f.exists())
{
JOptionPane.showMessageDialog(null, "File/Folder Does not exist", "Are You MAD...", JOptionPane.ERROR_MESSAGE);
}
else
{
if(f.exists() && JOptionPane.showConfirmDialog(null,"Are you sure you want to delete", "Hi..", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
deleteFile(f);
JOptionPane.showMessageDialog(null, "Deleted", "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
}

if(e.getSource()==browse)
{
int result=chooser.showOpenDialog(null);
if(result==chooser.APPROVE_OPTION)
{
tf.setText(chooser.getSelectedFile().getAbsolutePath());
//filename=chooser.getSelectedFile().getAbsolutePath();
}
}

if(e.getActionCommand()=="Clear")
{
tf.setText("");
}
}
public void deleteFile(File path) {
if( path.exists() ) {
if (path.isDirectory()) {
File[] files = path.listFiles();
for(int i=0; i if(files[i].isDirectory()) {
deleteFile(files[i]);
} else {
files[i].delete();
}
}
}
}
path.delete();
}
}

Download

0 comments:

Post a Comment