Hello, Code can be downloaded/found here: https://www.dropbox.com/sh/jr0xf5kog35
ID: 3551070 • Letter: H
Question
Hello,
Code can be downloaded/found here: https://www.dropbox.com/sh/jr0xf5kog35l4j3/T6APHA55Tn
I am having problems doing this. Would like to see an example of how to do this so that I can see what I am doing wrong.
Test your program for the following scenarios:
a. a non-existent folder (you can do this by typing an invalid path in the text box of the
interface, instead of choosing an existing directory)
b. an empty folder
c. a folder containing only one file
d. a folder containing only one subfolder
e. a folder containing multiple files (but no subfolders)
f. a folder containing multiple, non-nested subfolders (but no files)
g. a folder containing both files and subfolders (all folders are non-nested)
h. a folder containing multiple, nested subfolders (but no files)
i. a folder containing an arbitrary mix of files and subfolders (some nested, some nonnested)
Explanation / Answer
The call to showOpenDialog appears in the actionPerformed method of the Open a File button's action listener:
The showXxxDialog methods return an integer that indicates whether the user selected a file. Depending on how you use a file chooser, it is often sufficient to check whether the return value is APPROVE_OPTION and then not to change any other value. To get the chosen file (or directory, if you set up the file chooser to allow directory selections), call the getSelectedFile method on the file chooser. This method returns an instance of File.
The example obtains the name of the file and uses it in the log message. You can call other methods on the File object, such as getPath, isDirectory, or exists to obtain information about the file. You can also call other methods such as delete and rename to change the file in some way. Of course, you might also want to open or save the file by using one of the reader or writer classes provided by the Java platform. See Basic I/O for information about using readers and writers to read and write data to the file system.
The example program uses the same instance of the JFileChooser class to display a standard save dialog. This time the program calls showSaveDialog:
By using the same file chooser instance to display its open and save dialogs, the program reaps the following benefits:
Finally, the example program has commented-out lines of code that let you change the file selection mode. For example, the following line of code makes the file chooser able to select only directories, and not files:
Another possible selection mode is FILES_AND_DIRECTORIES. The default is FILES_ONLY. The following picture shows an open dialog with the file selection mode set toDIRECTORIES_ONLY. Note that, in the Java look and feel at least, only directories are visible
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.