Example usage for javax.swing JFileChooser getSelectedFiles

List of usage examples for javax.swing JFileChooser getSelectedFiles

Introduction

In this page you can find the example usage for javax.swing JFileChooser getSelectedFiles.

Prototype

public File[] getSelectedFiles() 

Source Link

Document

Returns a list of selected files if the file chooser is set to allow multiple selection.

Usage

From source file:org.pgptool.gui.ui.tools.browsefs.MultipleFilesChooserDialog.java

/**
 * Blocking call to browse for files/*from   ww w .j av  a2s  .com*/
 * 
 * @return null if nothing was chosen, or array of files chosen otherwise
 */
public File[] askUserForMultipleFiles() {
    JFileChooser ofd = buildFileChooserDialog();

    int result = ofd.showOpenDialog(optionalParent);
    if (result != JFileChooser.APPROVE_OPTION) {
        handleFilesWereChosen(null);
        return null;
    }
    File[] retFile = ofd.getSelectedFiles();
    if (retFile == null || retFile.length == 0) {
        handleFilesWereChosen(null);
        return null;
    }

    handleFilesWereChosen(retFile);
    configPairs.put(configPairNameToRemember,
            FilenameUtils.getFullPathNoEndSeparator(retFile[0].getAbsolutePath()));
    return retFile;
}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);//w w w . j av a  2  s  .  co  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                File[] sf = chooser.getSelectedFiles();
                String filelist = "nothing";
                if (sf.length > 0)
                    filelist = sf[0].getName();
                for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                }
                statusbar.setText("You chose " + filelist);
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You saved "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You opened "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(saveButton);
    c.add(dirButton);
    c.add(statusbar);
}

From source file:filesscanner.MainWindow.java

private void scanBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanBtnActionPerformed
    File f = new File("C:");
    JFileChooser chooser = ShowChooser(f);
    int res = chooser.showDialog(this, " ");

    if (res == JFileChooser.APPROVE_OPTION) {

        File[] files = chooser.getSelectedFiles();

        int key = 0;
        for (File file : files) {
            modelDirectories.add(key++, chooser.getSelectedFile().toString());
        }/*from   w  w  w  .  j a  va 2  s  .  c  o m*/

    }
}

From source file:org.pmedv.jake.commands.AddFilesCommand.java

@Override
public void execute() {

    final ApplicationContext ctx = AppContext.getApplicationContext();
    final ApplicationWindow win = (ApplicationWindow) ctx.getBean(BeanDirectory.WINDOW_APPLICATION);

    /**// w  ww  .  j a v a 2s.c  om
     * Get last selected folder to simplify file browsing
     */

    if (AppContext.getLastSelectedFolder() == null)
        AppContext.setLastSelectedFolder(System.getProperty("user.home"));

    JFileChooser fc = new JFileChooser(AppContext.getLastSelectedFolder());
    fc.setDialogTitle("Add files");
    fc.setMultiSelectionEnabled(true);
    fc.setFileFilter(new MP3Filter());

    int result = fc.showOpenDialog(win);

    if (result == JFileChooser.CANCEL_OPTION)
        return;

    File[] files = fc.getSelectedFiles();

    PlayerView view = JakeUtil.getCurrentActivePlayer();

    if (view == null) {

        ArrayList<File> fileList = new ArrayList<File>();

        for (int i = 0; i < files.length; i++)
            fileList.add(files[i]);

        final PlayerController controller = new PlayerController(fileList);
        JakeUtil.updateRecentFiles(fc.getSelectedFile().getAbsolutePath());
        AppContext.setLastSelectedFolder(fc.getSelectedFiles()[0].getParentFile().getAbsolutePath());

        View v = new View(fc.getSelectedFile().getAbsolutePath(), null, controller.getPlayerView());

        v.addListener(new DockingWindowAdapter() {

            @Override
            public void windowClosing(DockingWindow arg0) throws OperationAbortedException {

                controller.getPlayer().close();
                controller.getPlayFileCommand().setPlaying(false);
            }

        });

        openEditor(v);
    }

    else {

        if (files.length >= 1) {

            AppContext.setLastSelectedFolder(fc.getSelectedFiles()[0].getParentFile().getAbsolutePath());
            JakeUtil.updateRecentFiles(fc.getSelectedFiles()[0].getParentFile().getAbsolutePath());

            FileTableModel model = (FileTableModel) view.getFileTable().getModel();

            for (int i = 0; i < files.length; i++) {
                model.addObject(files[i]);
            }

        }

    }

}

From source file:cool.pandora.modeller.ui.handlers.base.AddDataHandler.java

/**
 * addData./*from w ww  . ja va2  s.  c  o  m*/
 */
void addData() {
    final File selectFile = new File(File.separator + ".");
    final JFrame frame = new JFrame();
    final JFileChooser fc = new JFileChooser(selectFile);
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setMultiSelectionEnabled(true);
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setDialogTitle("Add File or Directory");
    final int option = fc.showOpenDialog(frame);

    if (option == JFileChooser.APPROVE_OPTION) {
        final File[] files = fc.getSelectedFiles();
        final String message = ApplicationContextUtil.getMessage("bag.message.filesadded");
        if (files != null && files.length > 0) {
            addBagData(files);
            ApplicationContextUtil.addConsoleMessage(message + " " + getFileNames(files));
        } else {
            final File file = fc.getSelectedFile();
            addBagData(file);
            ApplicationContextUtil.addConsoleMessage(message + " " + file.getAbsolutePath());
        }
        bagView.bagPayloadTreePanel.refresh(bagView.bagPayloadTree);
        bagView.updateAddData();
    }
}

From source file:TextFileHandler.java

public File[] openFiles(String title) {
    File[] result = null;//from  w w w . ja va 2 s .c o  m
    JFileChooser chooser = new JFileChooser(new File("."));
    if (title != null)
        chooser.setDialogTitle(title);
    chooser.setMultiSelectionEnabled(true);
    int retVal = chooser.showOpenDialog(null);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        result = chooser.getSelectedFiles();
    }
    return result;
}

From source file:com.simplexrepaginator.RepaginateFrame.java

protected JButton createInputButton() {
    JButton b = new JButton("Click or drag to set input files", PDF_1342);

    b.setHorizontalTextPosition(SwingConstants.RIGHT);
    b.setIconTextGap(25);//ww  w .j av  a  2s. c om

    b.setTransferHandler(new InputButtonTransferHandler());

    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if (chooser.showOpenDialog(RepaginateFrame.this) != JFileChooser.APPROVE_OPTION)
                return;
            setInput(Arrays.asList(chooser.getSelectedFiles()));
            if (JOptionPane.showConfirmDialog(RepaginateFrame.this, "Use input paths as output paths?",
                    "Use Input As Output?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                setOutput(new ArrayList<File>(repaginator.getInputFiles()));
            }
        }
    });

    return b;
}

From source file:com.simplexrepaginator.RepaginateFrame.java

protected JButton creatOutputButton() {
    JButton b = new JButton("Click or drag to set output file", PDF_1234);

    b.setHorizontalTextPosition(SwingConstants.LEFT);
    b.setIconTextGap(25);/*from   ww  w  .j ava 2s  .  c o m*/

    b.setTransferHandler(new OutputButtonTransferHandler());

    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if (chooser.showOpenDialog(RepaginateFrame.this) != JFileChooser.APPROVE_OPTION)
                return;
            repaginator.setOutputFiles(Arrays.asList(chooser.getSelectedFiles()));
            output.setText("<html><center>" + StringUtils.join(repaginator.getOutputFiles(), "<br>"));
        }
    });

    return b;
}

From source file:org.martus.client.swingui.FxInSwingMainWindow.java

protected File[] showMultiFileOpenDialog(String title, File directory, Vector<FormatFilter> filters) {
    JFileChooser fileChooser = createFileChooser(title, directory, filters);
    fileChooser.setMultiSelectionEnabled(true);

    int userResult = fileChooser.showOpenDialog(getCurrentActiveFrame().getSwingFrame());
    if (userResult != JFileChooser.APPROVE_OPTION)
        return new File[0];

    return fileChooser.getSelectedFiles();
}

From source file:uk.ac.ox.cbrg.cpfp.uploadapp.UploadApplet.java

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed

    // Create & show file chooser with extension filter as per applet param
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new CustomFileFilter(fileExtensions));
    fc.setMultiSelectionEnabled(true);/*from ww w .j a  v a  2  s. co  m*/
    fc.showOpenDialog(UploadApplet.this);

    // Add selected files to upload queue
    File[] selFiles = fc.getSelectedFiles();
    addFiles(selFiles);

}