Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

JFileSelector



The JFileSelector class is a file chooser backed by a component. The important methods of the backing JFileChooser have associated methods in this class which defer to the underlying JFileChooser. For example:
jfileselector

Single and multiple selection

By default the JFileSelector only allows to perform a single file selection. The JFileSelector.getSelectedFile() method returns the selected file (or null if the selection was cancelled).

The JFileSelector.setMultiSelectionEnabled(boolean) allow to specify if the selector allows multiple files selection. The JFileSelector.getSelectedFiles() method returns the selected files (or null if the selection was cancelled).

If the JFileSelector only allows to perform a single file selection, the JFileSelector.getSelectedFiles() will return an array with one element which will be the selected file.

Sorting the files when using a multiple selection

It is possible to return the selected files in the order in which they were added by the user, by setting the JFileSelector.setDefaultSorted(boolean).

Protection against initialization delays

Note that the JFileSelector has a protection against possible very long delays when initializing the shared JFileChooser instance for the first time at the creation of the first JFileSelector instance (this is a problem which can happen in Java if there are disconnected drives in the Network).

The initialization of this shared JFileChooser instance is performed in a FutureTask in a background Thread and the resulting instance is only retrieved when the user effectively opens this underlying chooser. It means that most of the time, the delay will not be visible to the user.

Allowing to unselect a file

The JFileSelector uses a JOptionalFileChooser internally: For example:
      JFileSelector fs = new JFileSelector();
      fs.setHasOptionalFiles(true);
      fs.setCurrentDirectory(new File(System.getProperty("user.dir")));
      fs.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
           JFileChooser fs = (JFileChooser) e.getSource();
           System.out.println(fs.getSelectedFile());
         }
      });
In that case you will have the following GUI in the Popup:
optionalfilechooser
If you unset the file in the Popup, the resulting file will be null.

Text on the optional button

By default the JFileSelector GUI will use the default Locale language of the platform.

The following Locale languages are supported: The following Locale languages are supported:
  • English: en
  • French: fr
  • Italian: it
  • Spanish: sp
  • Portuguese: pt
  • German: de
  • Norwegian: no
  • Japanese: ja
  • Chinese: zh
If a Locale is not supported, the English Locale will be used. The text on the optional button depends on the Locale of the JFileSelector component. By default this is the default platform Locale, but it is possible to force the Locale by using the Component.setLocale(Locale) method on the JFileSelector component.

It is also possible to force the text on the button by using the JFileSelector.setUnselectFileText(String) method. For example:
   JFileSelector fs = new JFileSelector();
      fs.setHasOptionalFiles(true);
      fs.setUnselectedFileText("No File");
   fs.setCurrentDirectory(new File(System.getProperty("user.dir")));
   fs.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JFileChooser fs = (JFileChooser) e.getSource();
        System.out.println(fs.getSelectedFile());
      }
   });
You will have the following GUI in the Popup:
optionalfilechooser2

Forcing the file extension

The JFileSelector.setForceFileExtension(boolean) method allows to force the extension of the selected file for extensions specified with the JFileSelector.addChoosableFileFilter(FileFilter), in the case if the file filter is a ExtensionFileFilter[1]
See ExtensionFileFilter for more information about this file filter


If the extension is forced, and in the case where: Then the file will add the associated extension after the selection, with either the selection by the text field or the file selector.

For example:
   JFileSelector fs = new JFileSelector();
   fs.setFileSelectionMode(JFileChooser.FILES_ONLY);
   fs.setDialogType(JFileChooser.SAVE_DIALOG);
   ExtensionFileFilter filter = new ExtensionFileFilter("txt", "Text files");
   fs.addChoosableFileFilter(filter);
   fs.setForceFileExtension(true);      
If the selected file is "C:\my\text_file", the file will be set to "C:\my\text_file.txt".

If you don't type the full path of the file in the text field, the file selector will look for a file in the current directory. After pressing Enter, the full path of the file will be presented in the text field.

Example

The following code creates a JFileSelector:
   JFrame f = new JFrame("test");
   JFileSelector fs = new JFileSelector();
   fs.setCurrentDirectory(new File(System.getProperty("user.dir")));

   fs.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
       System.out.println("Action : ");
       JFileChooser chooser = (JFileChooser)e.getSource();
       File file = chooser.getSelectedFile();
     }
   });
   f.getContentPane().add(fs);
   f.pack();
   f.setVisible(true);

Customizing the text field content

The text field content for both the JFileSelector and the JMultipleFileSelector can be customized by providing a FileSelectorRenderer to the file selector. By default the selector uses a DefaultFileSelectorRenderer which will show the absolute paths of the selected files.

You can for example specify a custom FileSelectorRenderer which will shows paths relative to a parent directory rather than absolute.

Notes

  1. ^ See ExtensionFileFilter for more information about this file filter

See also


Categories: packages | swing

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences