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

Main Article: 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

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

Main Article: 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.

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

Categories: packages | swing

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