JFileChooser class:JFileChooser classtrue, then if will present an additional button in the interface to allow to unset the fileJOptionalFileChooser 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:
JOptionalFileChooser GUI will use the default Locale language of the platform.enfritspptdenojazhJOptionalFileChooser 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.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:
JOptionalFileChooser chooser = new JOptionalFileChooser(<current directory>, true);
JFileChooser have associated methods in this class which defer to the underlying JFileChooser. For example:
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:
JFileChooser, or the JFileSelector, each of the files can be selected in its specific directory.
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:
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences