Page 667 - Introduction to Programming with Java: A Problem Solving Approach
P. 667
15.10 GUI Track: The JFileChooser Class (Optional) 633
import java.io.File;
import javax.swing.*;
// for JFileChooser and JOptionPane;
public class FileSizesGUI
{
public static void main(String[] args)
{
}
// end main
File fileDir;
int response;
File[] files;
// user-specified file or directory
// user's response to GUI prompts
// array of files in specified directory
String output = ""; // list of filenames and sizes
JFileChooser chooser = new JFileChooser(".");
response = JOptionPane.showConfirmDialog(null,
"This program displays the filenames and file sizes of" +
" specified files.\nWould you like to run the program?",
"File Sizes", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION)
{
}
// end if
chooser.setFileSelectionMode(
JFileChooser.FILES_AND_DIRECTORIES);
response = chooser.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
{
}
// end if
Apago PDF Enhancer
fileDir = chooser.getSelectedFile();
if (fileDir.isFile())
{
}
output += String.format("%-25s%12s%n",
fileDir.getName(), fileDir.length() + " bytes");
else if (fileDir.isDirectory())
{
}
// end else
else
{
}
files = fileDir.listFiles();
for (int i=0; i<files.length; i++)
{
output += String.format("%-25s%12s%n",
files[i].getName(), files[i].length() + " bytes");
} // end for
output = "Invalid entry. Not a file or directory.";
JOptionPane.showMessageDialog(null, output,
"File Sizes", JOptionPane.INFORMATION_MESSAGE);
} // end FileSizesGUI class
Figure 15.16 FileSizesGUI program