Page 665 - Introduction to Programming with Java: A Problem Solving Approach
P. 665

                15.10 GUI Track: The JFileChooser Class (Optional) 631
When you call setFileSelectionMode, you pass in a mode argument to specify the type of file the user can choose. If the mode is JFileChooser.FILES_ONLY, the user is allowed to choose only a file. If the mode is JFileChooser.DIRECTORIES_ONLY, the user is allowed to choose only a direc- tory. If the mode is JFileChooser.FILES_AND_DIRECTORIES, the user is allowed to choose either a file or a directory.
You call showOpenDialog to display a file-chooser dialog box. After the dialog box displays, if the user clicks the file chooser’s Open button, the showOpenDialog method call returns the JFileChooser.APPROVE_OPTION named constant. If the user clicks the file chooser’s Cancel button, the showOpenDialog method call returns the JFileChooser.CANCEL_OPTION named constant.
After a user selects a file with the JFileChooser dialog box, the program calls getSelectedFile to retrieve the selected file or directory. At that point, the program will probably want to do something with the file or directory. But before it does, it should use File’s exists method to determine if the user’s entry is valid, and it should use File’s isFile or isDirectory method to determine the selected file’s type.
JOptionPane Usage
You may need to use some of the methods of the JOptionPane class also. This class is also in the javax. swing package, so if you imported this package for the JFileChooser class, you’ll have access to the JOptionPane class too. The JOptionPane class provides many useful class methods, which you can access directly with the class name. We’ll look at just two of them—the showConfirmDialog method and the showMessageDialog method. Here are the API headings and descriptions:
          Apago PDF Enhancer
Object message, String title, int optionType)
This brings up a dialog box in which the number of choices is determined by optionType. public static void showMessageDialog(Component parentComponent,
Object message, String title, int messageType)
This brings up a dialog box that displays a message.
When you call showConfirmDialog, you can use a reference to another frame within which you want the box displayed, or you can use just a null to position it relative to the whole screen. In addition, you may supply a text message, and you must supply a text title. For the optionType parameter, enter either JOptionPane.YES_NO_OPTION or JOptionPane.YES_NO_CANCEL_OPTION. The value returned is the option selected by the user, like JOptionPane.YES_OPTION or JOptionPane.NO_OPTION.
When you call showMessageDialog, you can use a reference to another frame within which you want the box displayed, or you can use just a null to position it relative to the whole screen. In addition, you may supply a text message, and you must supply a text title. For the messageType parameter, enter either JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane. WARNING_MESSAGE, JOptionPane.QUESTION_MESSAGE, or JOptionPane.PLAIN_MESSAGE.
FileSizesGUI Program
We’re now ready to incorporate these ideas in that improved FileSizes program we mentioned earlier. Our FileSizesGUI program uses a JFileChooser dialog box to retrieve a user-specified file or directory. If the user selects a file, the program displays the file’s name and size. If the user selects a directory, the program displays the filenames and sizes for all of the files in the directory. To get a better idea of how the program will operate, see the sample session in Figure 15.15. The program itself is in Figure 15.16.
public static int showConfirmDialog(Component parentComponent,



















































































   663   664   665   666   667