Product \ FileChooser

main API Code Example WebStart

Code Examples for the FileChooser widget

The FileChooser is a widget that can be used inside an already existing window or you can ask it to show itself in a new dialog directly. Using one of the static methods to show the chooser in a new dialog allows you to use so-called instance names. Using several instance-names for different parts of your application will automatically reuse settings such as last used directory.

Take for example an application that can open a picture and then the user can edit it and save it later. From several usability studies it has been shown that people expect two consequent open actions to allow them to open a file from the same directory, even if there was a save action to a different directory in between.

A File open method might be implemented like this:

        public void openPicture() {
            File file = FileChooser.selectFile(myFrame, "myApplication.file-open");
            if(file == null) // cancelled..
                return;
            // do rest of open.
        }

In case you want to restrict the listing of files to (for example) only jpeg files you can use the fact that they always end in '.jpg' and provide a filter to the dialog. There is another FileChooser.selectFile method that allows you to provide a list of fileFilters which the user can select in the resulting filechooser. Each fileFilter will display a specific subsection of the files present on the system. One fileFilter can show all files, another can limit it to only jpeg files, a thirth can show all known picture format, for example.
It is adviced to always provide a way to let the user see all files so you should always add the AllFilesFilter filefilter as the first filter.

Adding a jpeg file filter works like this:

        public void openPicture() {
            List fileFilters = new ArrayList();
            fileFilters.add(new AllFilesFilter());
            fileFilters.add(new UICFileFilter("Pictures", ".*\\.jpg"));
            File file = FileChooser.selectFile(myFrame, "myApplication.file-open",
                    fileFilters);
            if(file == null) // cancelled..
                return;
            // do rest of open.
        }