List of usage examples for javax.swing JFileChooser setFileSelectionMode
@BeanProperty(preferred = true, enumerationValues = { "JFileChooser.FILES_ONLY", "JFileChooser.DIRECTORIES_ONLY", "JFileChooser.FILES_AND_DIRECTORIES" }, description = "Sets the types of files that the JFileChooser can choose.") public void setFileSelectionMode(int mode)
JFileChooser
to allow the user to just select files, just select directories, or select both files and directories. From source file:nick.gaImageRecognitionGui.panel.JPanelTestSavedConfiguration.java
private void jButtonSelectFalseImagesFolder1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectFalseImagesFolder1ActionPerformed JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new java.io.File(".")); fc.setDialogTitle(StringConstants.SELECT_TEST_IMAGES_FOLDER); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setAcceptAllFileFilterUsed(false); if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { String path = fc.getSelectedFile().getPath(); RuntimeConfiguration.getInstance().setTestFalseImagesFolderLocation(path); testFalseImagesFolderLocation.setText(path); } else {//from ww w .j a va 2s. co m String path = RuntimeConfiguration.getInstance().getTestFalseImagesFolderLocation(); if (StringUtils.isNotBlank(path)) { testFalseImagesFolderLocation.setText(path); } } }
From source file:nick.gaImageRecognitionGui.panel.JPanelTraining.java
private void jButtonSelectTrainingFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectTrainingFolderActionPerformed JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new java.io.File(".")); fc.setDialogTitle(StringConstants.SELECT_TRAINING_FOLDER); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setAcceptAllFileFilterUsed(false); if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { String path = fc.getSelectedFile().getPath(); RuntimeConfiguration.getInstance().setTrainingFolderPath(path); trainingFolderLocation.setText(path); } else {//from w w w . j a v a2 s .c o m String path = RuntimeConfiguration.getInstance().getTrainingFolderPath(); if (StringUtils.isNotBlank(path)) { trainingFolderLocation.setText(path); } } }
From source file:nick.gaImageRecognitionGui.panel.JPanelTraining.java
private void jButtonSelectFalseImagesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectFalseImagesActionPerformed JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new java.io.File(".")); fc.setDialogTitle(StringConstants.SELECT_TRAINING_FOLDER); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setAcceptAllFileFilterUsed(false); if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { String path = fc.getSelectedFile().getPath(); RuntimeConfiguration.getInstance().setTrainingFalseImagesFolderPath(path); falseImagesFolderLoc.setText(path); } else {// w ww . j av a2s .co m String path = RuntimeConfiguration.getInstance().getTrainingFalseImagesFolderPath(); if (StringUtils.isNotBlank(path)) { falseImagesFolderLoc.setText(path); } } }
From source file:nl.b3p.applet.local.Files.java
/** * Show a directory selector dialog.//from w w w . j av a 2s . c o m * * @param title The title for the dialog. * @return the selected directory or null if the dialog was canceled */ public static String selectDirectory(Component component, String title) { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle(title); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (chooser.showOpenDialog(component) == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile().toString(); } else { return null; } }
From source file:nl.fontys.sofa.limo.view.project.actions.ExportChainAction.java
private void openFileChooser() throws HeadlessException, IOException { JFileChooser fc = new ChainSaveFileChooser(); FileNameExtensionFilter chainFilter = new FileNameExtensionFilter("Supply chains (*.lsc)", "lsc"); if (supplyChain.getFilepath() != null) { //This happens if a supply chain is loaded. fc.setCurrentDirectory(new File(supplyChain.getFilepath())); }// ww w .j a v a2 s . c om fc.setFileFilter(chainFilter); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setSelectedFile(new File(FilenameUtils.removeExtension(supplyChain.getName()))); // This sets the name without the extension int result = fc.showOpenDialog(null); String fileName = fc.getSelectedFile().getName(); //name with extension if (result == JFileChooser.APPROVE_OPTION) { //If folder is selected than save the supply chain. supplyChain.setName(fileName); File file = fc.getSelectedFile(); supplyChain.saveToFile(file.getAbsolutePath() + ".lsc"); } else { //If no folder is selected throw an exception so the saving process is cancelled. throw new IOException("The supply chain " + supplyChain.getName() + " is invalid."); } }
From source file:nl.tudelft.goal.SimpleIDE.SimpleIDE.java
/** * Opens file browser panel and asks user to select a filename/directory. * * @param parentpanel//from w ww. ja v a 2s.co m * the panel to center this file browser panel on. * @param openFile * boolean that must be set to true to open a file dialog, and to * false to open a save dialog. * @param title * the title for the file browser panel. * @param mode * can be set to FILES_ONLY or DIRECTORIES_ONLY or something else * to fix selection. * @param ext * the expected extension. If the extension is not starting with * a dot, we insert a dot. Is enforced (if not null) by simply * adding when user enters a filename without any extension, or * by requesting user to change it if it is not right. If * enforceExtension = true, we throw exception when user changes * extension. If false, we allow explicit differnt extension by * user. * @param defaultName * File name that will be suggested (if not null) by the dialog. * Should be of the form defaultName###.extension. Note that if * openFile is set to true then an existing label is picked of * that form, and if openFile is set to false then a new name is * picked. * @param startdir * is the suggested start location for the browse. If this is set * to null, {@link System#getProperty(String)} is used with * String="user.home". * @param enforceExtension * is set to true if extension MUST be used. false if extension * is only suggestion and can be changed. Only applicable if * extension is not null. * @return The selected file. Adds default extension if file path does not * have file extension. * @throws GOALCommandCancelledException * if user cancels action. * @throws GOALUserError * if user makes incorrect selection. * */ public static File askFile(Component parentpanel, boolean openFile, String title, int mode, String exten, String defaultName, String startdir, boolean enforceExtension) throws GOALCommandCancelledException, GOALUserError { // insert the leading dot if necessary. String extension = (exten == null || exten.startsWith(".")) ? exten //$NON-NLS-1$ : "." + exten; //$NON-NLS-1$ /** * File name to be returned. */ File selectedFile; /** * Return state of file chooser. */ int returnState; if (startdir == null) { startdir = System.getProperty("user.home"); //$NON-NLS-1$ } try { File dir = new File(startdir); String suggestion = null; if (defaultName != null) { if (openFile) { suggestion = findExistingFilename(dir, defaultName, extension); } else { suggestion = createNewNonExistingFilename(dir, defaultName, extension); } } JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(dir); chooser.setDialogTitle(title); chooser.setFileSelectionMode(mode); if (suggestion != null) { chooser.setSelectedFile(new File(suggestion)); } if (openFile) { returnState = chooser.showOpenDialog(parentpanel); } else { returnState = chooser.showSaveDialog(parentpanel); } // TODO: handle ALL return state options + exception here if (returnState != JFileChooser.APPROVE_OPTION) { throw new GOALCommandCancelledException("user cancelled open dialog."); //$NON-NLS-1$ // FIXME: why not just return null? // That's a clean way of saying that the user did not select // any file; cancelling is not a program-breaking offence. // (and null will be returned when the user disagrees // with overwriting an existing file anyway) } selectedFile = chooser.getSelectedFile(); if (openFile && !selectedFile.exists()) { // file browsers you can select // non-existing files! throw new FileNotFoundException("File " //$NON-NLS-1$ + selectedFile.getCanonicalPath() + " does not exist."); //$NON-NLS-1$ } } catch (FileNotFoundException e) { throw new GOALUserError(e.getMessage(), e); } catch (IOException e) { throw new GOALUserError(e.getMessage(), e); } catch (HeadlessException e) { throw new GOALUserError(e.getMessage(), e); } // Check extension. String ext = FilenameUtils.getExtension(selectedFile.getName()); if (extension != null) { if (ext.isEmpty()) { selectedFile = new File(selectedFile.getAbsolutePath() + extension); } else { // Check whether extension is OK. if (enforceExtension && !("." + ext).equals(extension)) { //$NON-NLS-1$ throw new GOALUserError("The file must have extension " //$NON-NLS-1$ + extension); } } } Extension fileExtension = Extension.getFileExtension(selectedFile.getName()); if (fileExtension == null) { throw new GOALUserError("Files with extension " + ext //$NON-NLS-1$ + " are not recognized by GOAL"); //$NON-NLS-1$ } // Check whether file name already exists. if (!openFile && selectedFile != null && selectedFile.exists()) { int selection = JOptionPane.showConfirmDialog(parentpanel, "Overwrite existing file?", "Overwrite?", //$NON-NLS-1$ //$NON-NLS-2$ JOptionPane.YES_NO_OPTION); if (selection != JOptionPane.YES_OPTION) { throw new GOALCommandCancelledException("User refused overwrite of file " + selectedFile); //$NON-NLS-1$ } } return selectedFile; }
From source file:nz.govt.natlib.ndha.manualdeposit.dialogs.ApplicationProperties.java
private void addFavourite() { final JFileChooser fc = new JFileChooser(); fc.setDialogTitle("Select favourite directory"); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); final int result = fc.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { final DefaultListModel model = (DefaultListModel) lstFavourites.getModel(); model.addElement(fc.getSelectedFile().getAbsolutePath()); }/*from w w w.jav a 2s.c o m*/ checkButtons(); }
From source file:op.controlling.PnlControlling.java
private JPanel createContentPanel4Hygiene() { JPanel pnlContent = new JPanel(new VerticalLayout()); JPanel pnlPrevalence = new JPanel(new BorderLayout()); final JButton btnPrevalence = GUITools.createHyperlinkButton("opde.controlling.hygiene.prevalence", null, null);//w ww. j a va 2 s .co m final JDateChooser jdc = new JDateChooser(new Date()); final JCheckBox cbAnonymous = new JCheckBox(SYSTools.xx("misc.msg.anon")); cbAnonymous.setSelected(true); jdc.setMaxSelectableDate(new Date()); btnPrevalence.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { MREPrevalenceSheets mre = new MREPrevalenceSheets(new LocalDate(), cbAnonymous.isSelected(), progressClosure); return mre.createSheet(); } @Override protected void done() { try { File source = (File) get(); Object[] options = { SYSTools.xx("prevalence.optiondialog.option1"), SYSTools.xx("prevalence.optiondialog.option2"), SYSTools.xx("opde.wizards.buttontext.cancel") }; int n = JOptionPane.showOptionDialog(OPDE.getMainframe(), SYSTools.xx("prevalence.optiondialog.question"), SYSTools.xx("prevalence.optiondialog.title"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); File copyTargetDirectory = null; if (n == 1) { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("title"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setMultiSelectionEnabled(false); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(pnlPrevalence) == JFileChooser.APPROVE_OPTION) { copyTargetDirectory = chooser.getSelectedFile(); } } if (copyTargetDirectory != null) { FileUtils.copyFile(source, copyTargetDirectory); } else { if (n == 0) { SYSFilesTools.handleFile((File) get(), Desktop.Action.OPEN); } } } catch (Exception e) { OPDE.fatal(e); } OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); pnlPrevalence.add(btnPrevalence, BorderLayout.WEST); JPanel optionPanel = new JPanel(); optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.LINE_AXIS)); optionPanel.add(cbAnonymous); optionPanel.add(jdc); pnlPrevalence.add(optionPanel, BorderLayout.EAST); pnlContent.add(pnlPrevalence); return pnlContent; }
From source file:org.andrewberman.sync.PDFDownloader.java
static File chooseDirectory() throws Exception { JFileChooser fc = new JFileChooser(); // fc.setDialogTitle("Choose Output Directory"); // Start in current directory fc.setCurrentDirectory(new File(".")); // Choose only directories only. fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // Open chooser dialog int result = fc.showSaveDialog(new JFrame()); if (result == JFileChooser.CANCEL_OPTION) { out.println("Directory choice was canceled. Exiting..."); // System.exit(1); throw new Exception(); }/*w ww.j a v a 2s. c o m*/ File f = fc.getSelectedFile(); out.println("Chosen directory:" + f); return f; }
From source file:org.apache.cayenne.modeler.action.ImportDataMapAction.java
protected File selectDataMap(Frame f) { // find start directory in preferences FSPath lastDir = getApplication().getFrameController().getLastDirectory(); // configure dialog JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); lastDir.updateChooser(chooser);// w w w .j a v a2 s.c o m chooser.addChoosableFileFilter(FileFilters.getDataMapFilter()); int status = chooser.showDialog(f, "Select DataMap"); if (status == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); // save to preferences... lastDir.updateFromChooser(chooser); return file; } return null; }