Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

File choosers



Several classes extend the Swing JFileChooser class:

JOptionalFileChooser

The JOptionalFileChooser class is a file chooser which allows to unselect a file.

For example, for the following code:
      JOptionalFileChooser fileChooser = new JOptionalFileChooser();
      fileChooser.setHasOptionalFiles(true);
      File selFile = new File("D:\\Java\\mdiutilities\\README");
      fileChooser.setSelectedFile(selFile);
      fileChooser.setDialogTitle("select file");
      if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
         File file = fileChooser.getSelectedFile();
         System.out.println("file: " + file);
      }
You will have the following GUI:
optionalfilechooser
If you unset the file in the GUI, the resulting file will be null.

Text on the optional button

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

It is also possible to force the text on the button by using the JOptionalFileChooser.setUnselectFileText(String) method. For example:
      JOptionalFileChooser fileChooser = new JOptionalFileChooser();
      fileChooser.setHasOptionalFiles(true);
      File selFile = new File("D:\\Java\\mdiutilities\\README");
      fileChooser.setSelectedFile(selFile);
      fileChooser.setUnselectFileText("No File");
      fileChooser.setDialogTitle("select file");
      if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
         File file = fileChooser.getSelectedFile();
         System.out.println("file: " + file);
      }
You will have the following GUI:
optionalfilechooser2

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

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

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);
You will obtain the following result:
jfileselector

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

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.

Categories: packages | swing

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