Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

JMultipleFileSelector



The JMultipleFileSelector class is a multiple file chooser backed by a component, where the order of the files is clearly specified by the user. Contrary to the standard Java JFileChooser, or the JFileSelector, each of the files can be selected in its specific directory.

Using the text field to determine the files

You can use the text field to specify the file. For example:
jmultiplefileselector2

Protection against initialization delays

The JMultipleFileSelector uses JFileSelectors internally so the JMultipleFileSelector is also protected against long delays when initializing the shared JFileChooser instance for the first time.

Example

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

   fs.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
       System.out.println("Action : ");
       JMultipleFileSelector selector = (JMultipleFileSelector)e.getSource();
       File[] files = selector.getSelectedFiles();
     }
   });

   f.getContentPane().add(fs);
   f.pack();
   f.setVisible(true);
You will obtain the following result:
jmultiplefileselector

Allowing to unselect a file

It is possible to add an option in the for the JMultipleFileSelector to add an option to unset the file list: For example:
      JFrame f = new JFrame("test");
      JMultipleFileSelector fs = new JMultipleFileSelector();
      fs.setHasOptionalFiles(true);
      fs.setCurrentDirectory(new File(System.getProperty("user.dir")));

      fs.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          System.out.println("Action : ");
          JMultipleFileSelector selector = (JMultipleFileSelector)e.getSource();
          File[] files = selector.getSelectedFiles();
        }
      });

      f.getContentPane().add(fs);
      f.pack();
      f.setVisible(true);
In that case you will have the following GUI in the Popup:
jmultiplefileselectoroptional
If you unset the files in the Popup, the resulting files will be an empty array of files.

Text on the optional button

By default the JMultipleFileSelector 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 JMultipleFileSelector 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 JMultipleFileSelector component.

It is also possible to force the text on the button by using the JMultipleFileSelector.setUnselectFilesText(String) method. For example:
   JFrame f = new JFrame("test");
   JMultipleFileSelector fs = new JMultipleFileSelector();
      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) {
       System.out.println("Action : ");
       JMultipleFileSelector selector = (JMultipleFileSelector)e.getSource();
       File[] files = selector.getSelectedFiles();
     }
   });

   f.getContentPane().add(fs);
   f.pack();
   f.setVisible(true);
You will have the following GUI in the Popup:
jmultiplefileselectoroptional2

Forcing the file extension

Main Article: JFileSelector

The JMultipleFileSelector.setForceFileExtension(boolean) method allows to force the extension of the selected file for the JFileSelector used in this widget.

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.

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