Home
Username:
Password:
Java 6 Tutorials

Swing Components / Choosing Files

Subtitles of the Movie

One very common dialog is the file chooser. Every application of any size uses a dialog to let the user select a file to be processed and the name of a file to save data to disk. This dialog performs a simple function, one that everyone knows and maybe that's why it's so hard to write. Fortunately, one is included with the Java Swing set of dialogs. Here is an example demonstrating the two ways of using it to select a file to open for input or to select the name of a file to be written to. To be able to use all its features, you'll need to import some special classes. The file chooser is the dialog itself. The filter classes are used to control the names of the files that are displayed in the list of choices. And the file class holds the description of the chosen file. You can wait until the dialog is needed before you instantiate it or you can create the dialog ahead of time as I've done here. Creating the object is very simple. There are no arguments on the constructor, however, you will need to set the selection mode. Here you see that I set it to display both files and directories. You can also set it for files only or for directories only. The default is to display file names only, which can be quite handy if you don't want the user to browse the disk for files. You can further limit the files displayed at any one time to those that end with a specific extension. The extension of a file is the group of characters following the last period. The files without periods are considered not to have extensions. This example uses a filter for jpg images and another for tif images. You can add as many as you like. Each one has a descriptive string and one or more possible extension strings. The user can select the filter to be used. There is one filter automatically included that displays all the files. Directory names are exempt from filtering. This is the action performed method. It's called whenever you select a button. This example has only two buttons; one for open, another for save. The action for the two buttons is a little different. When the open button is selected, a call is made to the dialog method, show open dialog. This causes the dialog to appear with a label open on the bar at the top. The dialog has two buttons; open and cancel. If the user selects a file and chooses open, the return value is approve option and we need to get the file information, otherwise no action is taken. To get the name of a file, a call is made to get selected file and the file object is returned. In this example, all that happens is its name is printed. Now, the action for save is a little different. The name of a selected file is set into the dialog. Now, this isn't absolutely necessary, but quite often you already have a file name and you want to include it as the default option. A call is then made to show saved dialog, which displays the dialog with the word save on the top bar and the two buttons save and cancel. The chosen file name is displayed and will be the one returned with the save button. Here's what the example looks like. The file names that meet the filters requirement are displayed in the window. You can change the filter any time you want. When you select a file name, it appears in the file name list at the bottom. By the way, look at this. The file name remains in the slot because if you'll remember, this example created the dialog and keeps it for the duration of the life of the program. So it's like an elephant; it doesn't forget. However, when you open it with a save button, it puts a new file name in there. Notice it also has different buttons and the word save on the bar up at the top. Now, that's the basic function of the file chooser. You can do some other things like icons next to the file names and allow for multiple file name selections, but the basic operation is quite simple.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available