Java Code Examples for javax.swing.JFileChooser.getSelectedFiles()

The following are Jave code examples for showing how to use getSelectedFiles() of the javax.swing.JFileChooser class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: LibraryStartVisualPanel.java   Source Code and License Vote up 7 votes
private void browseLibraryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseLibraryButtonActionPerformed
    JFileChooser chooser = new JFileChooser(ModuleUISettings.getDefault().getLastChosenLibraryLocation());
    File[] olds = convertStringToFiles(txtLibrary.getText().trim());
    chooser.setSelectedFiles(olds);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(true);
    chooser.addChoosableFileFilter(new JarZipFilter());
    int ret = chooser.showDialog(this, getMessage("LBL_Select"));
    if (ret == JFileChooser.APPROVE_OPTION) {
        File[] files =  chooser.getSelectedFiles();
        if (files.length == 0) {
            return;
        }
        String path = "";
        for (int i = 0; i < files.length; i++) {
            path = path + files[i] + ( i == files.length - 1 ? "" : File.pathSeparator);
        }
        txtLibrary.setText(path);
        ModuleUISettings.getDefault().setLastChosenLibraryLocation(files[0].getParentFile().getAbsolutePath());
    }
}
 
Example 2
Project: Websocket-Smart-Card-Signer   File: SignUI.java   Source Code and License Vote up 7 votes
public static List<Data> showFileSelection() throws Exception{
    List<Data> ret = new ArrayList<Data>();
    
    JFileChooser jfc = new JFileChooser();
    jfc.setMultiSelectionEnabled(true);
    
    jfc.setDialogTitle("Choose the files to sign");
    SignUtils.playBeeps(1);
    if(jfc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
        return null;
    
    File[] choosedFileList = jfc.getSelectedFiles();
    for(File file:choosedFileList){
        String id = file.getAbsolutePath();
        byte[] fileContent = IOUtils.readFile(file);
        ret.add(new Data(id, fileContent));
    }
    return ret;
}
 
Example 3
Project: incubator-netbeans   File: OutputPanel.java   Source Code and License Vote up 6 votes
private void addOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOutputActionPerformed
    JFileChooser chooser = new JFileChooser();
    FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
    chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setMultiSelectionEnabled(true);
    if (lastChosenFile != null) {
        chooser.setSelectedFile(lastChosenFile);
    } else {
        File files[] = model.getBaseFolder().listFiles();
        if (files != null && files.length > 0) {
            chooser.setSelectedFile(files[0]);
        } else {
            chooser.setSelectedFile(model.getBaseFolder());
        }
    }
    chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Output")); // NOI18N
    if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
        for (File file : chooser.getSelectedFiles()) {
            file = FileUtil.normalizeFile(file);
            listModel.addElement(file.getAbsolutePath());
            lastChosenFile = file;
        }
        applyChanges();
        updateButtons();
    }
}
 
Example 4
Project: Equella   File: UploadAction.java   Source Code and License Vote up 6 votes
@Override
public void actionPerformed(ActionEvent e)
{
	JFileChooser fc = new JFileChooser(lastSelectedDirectory);
	fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	fc.setMultiSelectionEnabled(true);

	if( fc.showOpenDialog(fileList) == JFileChooser.APPROVE_OPTION )
	{
		File[] selectedFiles = fc.getSelectedFiles();
		if( selectedFiles != null && selectedFiles.length > 0 )
		{
			lastSelectedDirectory = selectedFiles[0];
			if( !lastSelectedDirectory.isDirectory() )
			{
				lastSelectedDirectory = lastSelectedDirectory.getParentFile();
			}

			uploadFiles(fileList.getCurrentDirectory(), selectedFiles);
		}
	}
}
 
Example 5
Project: geomapapp   File: ImportImage.java   Source Code and License Vote up 6 votes
void open() throws IOException {
	JFileChooser chooser = haxby.map.MapApp.getFileChooser();
	int mode = chooser.getFileSelectionMode();
	boolean multi = chooser.isMultiSelectionEnabled();
	chooser.setMultiSelectionEnabled( true );
	chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
	chooser.addChoosableFileFilter(imageFileFilter);
	
	int ok = chooser.showOpenDialog(frame);
	File[] choice = null;
	if( ok!=chooser.CANCEL_OPTION ) choice = chooser.getSelectedFiles();
	chooser.setMultiSelectionEnabled(multi);
	chooser.setFileSelectionMode( mode );
	chooser.removeChoosableFileFilter(imageFileFilter);

	if( ok==chooser.CANCEL_OPTION ) {
		return;
	}
	
	if (mapType == MapApp.MERCATOR_MAP)
		openImagesMercator(choice);
	else
		openImagesPolar(choice);
}
 
Example 6
Project: incubator-netbeans   File: SpringCustomizerPanel.java   Source Code and License Vote up 5 votes
private void addFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFileButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);
        chooser.setDialogTitle(NbBundle.getMessage(SpringCustomizerPanel.class, "LBL_ChooseFile")); //NOI18N
        chooser.setCurrentDirectory(basedir);
        int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(groupFilesList));
        if (option == JFileChooser.APPROVE_OPTION) {
            boolean showDialog = false;
            List<File> newFiles = new LinkedList<File>();
            StringBuilder existing = new StringBuilder(
                    NbBundle.getMessage(SpringCustomizerPanel.class, "LBL_FileAlreadyAdded")).append("\n"); //NOI18N
            for (File file : chooser.getSelectedFiles()) {
                if (files.contains(file)) {
                    existing.append(file.getAbsolutePath()).append("\n"); //NOI18N
                    showDialog = true;
                } else {
                    newFiles.add(file);
                }
            }

            // remember last location
            basedir = chooser.getCurrentDirectory();
            addFiles(newFiles);
            if (showDialog) {
                DialogDisplayer.getDefault().notify(
                        new NotifyDescriptor.Message(existing.toString(), NotifyDescriptor.ERROR_MESSAGE));
            }
        }
}
 
Example 7
Project: incubator-netbeans   File: FileChooserBuilder.java   Source Code and License Vote up 5 votes
/**
 * Show an open dialog that allows multiple selection.
 * @return An array of files, or null if the user cancelled the dialog
 */
public File[] showMultiOpenDialog() {
    JFileChooser chooser = createFileChooser();
    chooser.setMultiSelectionEnabled(true);
    int result = chooser.showOpenDialog(findDialogParent());
    if (JFileChooser.APPROVE_OPTION == result) {
        File[] files = chooser.getSelectedFiles();
        return files == null ? new File[0] : files;
    } else {
        return null;
    }
}
 
Example 8
Project: incubator-netbeans   File: OpenFileAction.java   Source Code and License Vote up 5 votes
/**
 * Displays the specified file chooser and returns a list of selected files.
 *
 * @param  chooser  file chooser to display
 * @return  array of selected files,
 * @exception  org.openide.util.UserCancelException
 *                     if the user cancelled the operation
 */
public static File[] chooseFilesToOpen(JFileChooser chooser)
        throws UserCancelException {
    File[] files;
    do {
        int selectedOption = chooser.showOpenDialog(
            WindowManager.getDefault().getMainWindow());
        
        if (selectedOption != JFileChooser.APPROVE_OPTION) {
            throw new UserCancelException();
        }
        files = chooser.getSelectedFiles();
    } while (files.length == 0);
    return files;
}
 
Example 9
Project: marathonv5   File: RFileChooser.java   Source Code and License Vote up 5 votes
private void recordApproveSelection(JFileChooser fc) {
    if (fc.isMultiSelectionEnabled()) {
        File[] fs = fc.getSelectedFiles();
        recorder.recordSelect(this, ChooserHelper.encode(fs));
    } else {
        File file = fc.getSelectedFile();
        recorder.recordSelect(this, ChooserHelper.encode(file));
    }
}
 
Example 10
Project: call-IDE   File: InsMain.java   Source Code and License Vote up 5 votes
/**
 * choose the files for moss
 */
private void chooseMossFileBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseMossFileBtnActionPerformed
    // TODO add your handling code here:
    JFileChooser chooseMoss = new JFileChooser();
    chooseMoss.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooseMoss.setMultiSelectionEnabled(true);
    chooseMoss.showOpenDialog(this);
    
    allMossFiles = chooseMoss.getSelectedFiles();
    for(File f : allMossFiles) {
        addedFilesArea.append(f.getAbsolutePath()+"\n");
    }
}
 
Example 11
Project: Cognizant-Intelligent-Test-Scripter   File: ReportDefect.java   Source Code and License Vote up 5 votes
/**
 * browse and select the attachment files
 */
private void selectFile() {
    JFileChooser chooser = new JFileChooser(com.cognizant.cognizantits.ide.main.explorer.settings.Settings.getScreenShotLoc());
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(this);
    File[] files = chooser.getSelectedFiles();
    for (File f : files) {
        attachments.setText(attachments.getText() + f.getAbsolutePath() + File.pathSeparator);
    }
}
 
Example 12
Project: iTrace-Archive   File: XMLBasicFixationFilter.java   Source Code and License Vote up 5 votes
@Override
public File[] filterUI() {
	JFileChooser chooser = new JFileChooser();
	FileNameExtensionFilter filter = new FileNameExtensionFilter(
			"XML Files", "xml");
	chooser.setFileFilter(filter);
	chooser.setMultiSelectionEnabled(true);
	int returnVal = chooser.showOpenDialog(null);
	if(returnVal == JFileChooser.APPROVE_OPTION) {
		return chooser.getSelectedFiles();
	} else {
		return null;
	}
}
 
Example 13
Project: iTrace-Archive   File: JSONBasicFixationFilter.java   Source Code and License Vote up 5 votes
@Override
public File[] filterUI() {
	JFileChooser chooser = new JFileChooser();
	FileNameExtensionFilter filter = new FileNameExtensionFilter(
			"JSON Files", "json");
	chooser.setFileFilter(filter);
	chooser.setMultiSelectionEnabled(true);
	int returnVal = chooser.showOpenDialog(null);
	if(returnVal == JFileChooser.APPROVE_OPTION) {
		return chooser.getSelectedFiles();
	} else {
		return null;
	}
}
 
Example 14
Project: iTrace-Archive   File: OldJSONBasicFixationFilter.java   Source Code and License Vote up 5 votes
@Override
public File[] filterUI() {
	JFileChooser chooser = new JFileChooser();
	FileNameExtensionFilter filter = new FileNameExtensionFilter(
			"JSON Files", "json");
	chooser.setFileFilter(filter);
	chooser.setMultiSelectionEnabled(true);
	int returnVal = chooser.showOpenDialog(null);
	if(returnVal == JFileChooser.APPROVE_OPTION) {
		return chooser.getSelectedFiles();
	} else {
		return null;
	}
}
 
Example 15
Project: iTrace-Archive   File: OldXMLBasicFixationFilter.java   Source Code and License Vote up 5 votes
@Override
public File[] filterUI() {
	JFileChooser chooser = new JFileChooser();
	FileNameExtensionFilter filter = new FileNameExtensionFilter(
			"XML Files", "xml");
	chooser.setFileFilter(filter);
	chooser.setMultiSelectionEnabled(true);
	int returnVal = chooser.showOpenDialog(null);
	if(returnVal == JFileChooser.APPROVE_OPTION) {
		return chooser.getSelectedFiles();
	} else {
		return null;
	}
}
 
Example 16
Project: Ultraino   File: FileUtils.java   Source Code and License Vote up 5 votes
public static String[] selectFiles(Component component,String name,String extension,File pathToUse){
       JFileChooser chooser = new JFileChooser();
       chooser.setMultiSelectionEnabled(true);
       if(extension != null){
           chooser.setFileFilter(new ExtensionFilter(extension));
       }
       boolean useIndicated = pathToUse != null;
       if (useIndicated && lastIndicatedPath == null){
           lastIndicatedPath = pathToUse;
       }

       chooser.setCurrentDirectory(useIndicated?lastIndicatedPath:lastChooserPath);
int result = chooser.showDialog(component, name);
       if ( result == JFileChooser.APPROVE_OPTION){
           try{
               if(useIndicated){
                   lastIndicatedPath = chooser.getCurrentDirectory();
               }else{
                   lastChooserPath = chooser.getCurrentDirectory();
               }

               File[] files = chooser.getSelectedFiles();
               String[] forReturn = new String[files.length];
               for(int i = 0; i < files.length; i++){
                   forReturn[i] = files[i].getAbsolutePath();
               }
               return forReturn;
           }catch(Exception e){e.printStackTrace();}
       }

       return null;
   }
 
Example 17
Project: Install_Builder_Universal   File: OpenFile.java   Source Code and License Vote up 5 votes
public File[] open(final JPanel panel) {
	JFileChooser fc = new JFileChooser();
	fc.setMultiSelectionEnabled(true);
	fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
	fc.setDialogTitle("Choose File(s)");
	fc.showOpenDialog(panel);
       return fc.getSelectedFiles();
}
 
Example 18
Project: incubator-netbeans   File: J2SEPlatformCustomizer.java   Source Code and License Vote up 4 votes
private static boolean select(
    final PathModel model,
    final File[] currentDir,
    final Component parentComponent) {
    final JFileChooser chooser = new JFileChooser ();
    chooser.setMultiSelectionEnabled (true);
    String title = null;
    String message = null;
    String approveButtonName = null;
    String approveButtonNameMne = null;
    if (model.type == SOURCES) {
        title = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenSources");
        message = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_Sources");
        approveButtonName = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenSources");
        approveButtonNameMne = NbBundle.getMessage (J2SEPlatformCustomizer.class,"MNE_OpenSources");
    } else if (model.type == JAVADOC) {
        title = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenJavadoc");
        message = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_Javadoc");
        approveButtonName = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenJavadoc");
        approveButtonNameMne = NbBundle.getMessage (J2SEPlatformCustomizer.class,"MNE_OpenJavadoc");
    }
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText(approveButtonName);
    chooser.setApproveButtonMnemonic (approveButtonNameMne.charAt(0));
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    if (Utilities.isMac()) {
        //New JDKs and JREs are bundled into package, allow JFileChooser to navigate in
        chooser.putClientProperty("JFileChooser.packageIsTraversable", "always");   //NOI18N
    }
    //#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
    chooser.setAcceptAllFileFilterUsed( false );
    chooser.setFileFilter (new ArchiveFileFilter(message,new String[] {"ZIP","JAR"}));   //NOI18N
    if (currentDir[0] != null) {
        chooser.setCurrentDirectory(currentDir[0]);
    }
    if (chooser.showOpenDialog(parentComponent) == JFileChooser.APPROVE_OPTION) {
        File[] fs = chooser.getSelectedFiles();
        boolean addingFailed = false;
        for (File f : fs) {
            //XXX: JFileChooser workaround (JDK bug #5075580), double click on folder returns wrong file
            // E.g. for /foo/src it returns /foo/src/src
            // Try to convert it back by removing last invalid name component
            if (!f.exists()) {
                File parent = f.getParentFile();
                if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) {
                    f = parent;
                }
            }
            if (f.exists()) {
                addingFailed|=!model.addPath (f);
            }
        }
        if (addingFailed) {
            new NotifyDescriptor.Message (NbBundle.getMessage(J2SEPlatformCustomizer.class,"TXT_CanNotAddResolve"),
                    NotifyDescriptor.ERROR_MESSAGE);
        }
        currentDir[0] = FileUtil.normalizeFile(chooser.getCurrentDirectory());
        return true;
    }
    return false;
}
 
Example 19
Project: incubator-netbeans   File: FileArrayEditor.java   Source Code and License Vote up 4 votes
/** Property change listaner attached to the JFileChooser chooser. */
public void propertyChange(PropertyChangeEvent e) {
    // Fix for IZ#36742 - FileArrayEditor fires each selection change twice
    if ( ANCESTOR.equals( e.getPropertyName()) ){
        myPropertyFired.set( null );
    }
    
    if (e.getSource() instanceof JFileChooser) {
        JFileChooser jfc = (JFileChooser) e.getSource();
        if (mode == jfc.DIRECTORIES_ONLY && jfc.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName())) {
            if (jfc.getSelectedFile() == null) {
                File dir = jfc.getCurrentDirectory();
                if (dir != null) {
                    setValue (new File[] {new File(dir.getAbsolutePath())});
                    return;
                }
            }
        }
    }
    
    if (( ! JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e.getPropertyName())) && 
        ( ! JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName()))) {
            return;
    }
    if (! (e.getSource() instanceof JFileChooser)) {
        return;
    }
    JFileChooser chooser = (JFileChooser)e.getSource();
    File[] f = (File[])chooser.getSelectedFiles();
    if (f == null) {
        return;
    }
    
    // Fix for IZ#36742 - FileArrayEditor fires each selection change twice
    if ( isAlreadyHandled ( chooser , f , e.getPropertyName())){
        return;
    }
    
    if ((f.length == 0) && (chooser.getSelectedFile() != null)) {
        f = new File[] { chooser.getSelectedFile() };
    }
    
    for (int i = 0; i < f.length; i++) {
        if (!files && f[i].isFile ()) return;
        if (!directories && f[i].isDirectory ()) return;
    }

    if (baseDirectory != null) {
        for (int i = 0; i < f.length; i++) {
            String rel = FileEditor.getChildRelativePath(baseDirectory, f[i]);
            if (rel != null) {
                f[i] = new File(rel);
            }
        }
    }
    
    File[] nf = new File[f.length];
    for (int i = 0; i < f.length; i++) {
        // the next line is
        // workaround for JDK bug 4533419
        // it should be returned back to f[i] after the
        // mentioned bug is fixed in JDK.
        nf[i] = new File(f[i].getAbsolutePath());
    }
    setValue(nf);
    
    FileEditor.lastCurrentDir = chooser.getCurrentDirectory();
}
 
Example 20
Project: incubator-netbeans   File: NbClassPathCustomEditor.java   Source Code and License Vote up 4 votes
private void addJarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJarButtonActionPerformed

        JFileChooser chooser = FileEditor.createHackedFileChooser();
        chooser.setFileHidingEnabled(false);
        setHelpToChooser( chooser );
        
        chooser.setFileFilter(new FileFilter() {
                                  public boolean accept(File f) {
                                      return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); // NOI18N
                                  }
                                  public String getDescription() {
                                      return getString("CTL_JarArchivesMask");
                                  }
                              });

        if (lastJarFolder != null) {
            chooser.setCurrentDirectory(lastJarFolder);
        }

        chooser.setDialogTitle(getString("CTL_FileSystemPanel.Jar_Dialog_Title"));
        chooser.setMultiSelectionEnabled( true );
        if (chooser.showDialog(this,
                               getString("CTL_Approve_Button_Title"))
                == JFileChooser.APPROVE_OPTION) {
            File[] files = chooser.getSelectedFiles();
            boolean found = false;
            for (int i=0; i<files.length; i++) {
                if ((files[i] != null) && (files[i].isFile())) {
                    found = true;
                    String path = files[i].getAbsolutePath();
                    if (! listModel.contains (path)) {
                        listModel.addElement (path);
                    }
                }
            }
            if ( found ) {
                lastJarFolder = chooser.getCurrentDirectory();
                fireValueChanged();
            }
            pathList.setSelectedIndex(listModel.size() - 1);
        }
  }