JFileChooser
have associated methods in this class which defer to the underlying JFileChooser
. For example:JFileSelector
only allows to perform a single file selection. The JFileSelector.getSelectedFile() method returns the selected file (or null
if the selection was cancelled).null
if the selection was cancelled).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).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.
JFileSelector
uses a JOptionalFileChooser internally:true
, then if will present an additional button in the interface to allow to unset the fileJFileSelector 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:
JFileSelector
GUI will use the default Locale language of the platform.en
fr
it
sp
pt
de
no
ja
zh
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.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:
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".
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);
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.FileSelectorRenderer
which will shows paths relative to a parent directory rather than absolute. Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences