Please send questions to
st10@humboldt.edu .
// can I call my Delete from another Java class?
// (not an applet --- they are normally not permitted
// to delete files, etc.!)
//
// last modified: 10-27-00
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class UseDelete
{
public static void main(String args[])
{
UseDeleteFrame thisFrame = new UseDeleteFrame();
thisFrame.setSize(600, 400);
thisFrame.setTitle("Using Our Delete Class");
thisFrame.show();
}
}
class UseDeleteFrame extends JFrame implements ActionListener
{
JLabel title, fileToDeleteLabel;
JTextArea filesList;
JScrollPane filesListPane;
JTextField fileToDeleteField;
String currentDirectory;
File currDirFile;
public UseDeleteFrame()
{
Container myContentPane;
myContentPane = getContentPane();
myContentPane.setLayout(new FlowLayout());
// gracefully handle a window closing event
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// this text area will hold names of files in current
// directory
filesList = new JTextArea(10, 20);
// if I put this JTextArea into a JScrollPane
// in this way, then
// the scroll pane will add scroll bars when
// needed...!
filesListPane = new JScrollPane(filesList);
myContentPane.add(filesListPane);
// here's how you can get the name of
// the current working directory!
currentDirectory = System.getProperty("user.dir");
System.out.println("currentDirectory: " + currentDirectory);
// get File object for current directory
currDirFile = new File(currentDirectory);
// this local method gets the files in currDirFile and
// displays them in a JTextArea one
// file name per line
fillFilesList(currDirFile, filesList);
fileToDeleteLabel = new JLabel("type file to delete: ");
myContentPane.add(fileToDeleteLabel);
fileToDeleteField = new JTextField(20);
fileToDeleteField.addActionListener(this);
myContentPane.add(fileToDeleteField);
}
//-----
// display the names of files in the directory corresponding
// to currDir in the text area currFilesList
//-----
private void fillFilesList(File currDir, JTextArea currFilesList)
{
String fileNamesToPrint;
String[] filesInCurrDir;
// what are the names of the files in
// the current directory, at this point?
filesInCurrDir = currDir.list();
// put each in a string, each followed by a
// newline character
fileNamesToPrint = "";
for (int i = 0; i < filesInCurrDir.length; i++)
{
fileNamesToPrint += filesInCurrDir[i] + "\n";
}
// make that the parameter text area's contents
currFilesList.setText(fileNamesToPrint);
}
public void actionPerformed(ActionEvent e)
{
String fileNameToDelete;
fileNameToDelete = fileToDeleteField.getText();
// can I call my Delete.java method delete()?
// (I think it is because it is static that it can
// be called this way)
//
// note that, since it CAN throw an exception I
// need to call it in a try-catch block!
try
{
Delete.delete(fileNameToDelete);
// deleted file should now be gone from
// text area filesList
fillFilesList(currDirFile, filesList);
}
catch (IllegalArgumentException exc)
{
System.out.println(exc.getMessage());
}
// whether delete succeeds or fails, I think I
// should clear the input textfield...
fileToDeleteField.setText("");
}
}