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:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(null);/*from  w  w w .j ava2  s .c  om*/

    File[] files = chooser.getSelectedFiles();
}

From source file:Main.java

public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    int status = fileChooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        File[] selectedFiles = fileChooser.getSelectedFiles();
    } else if (status == JFileChooser.CANCEL_OPTION) {
        System.out.println("canceled");
    }//from w  w w.  j  a  va  2  s  . co  m
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);/*  www.  j  av a 2s.  com*/
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();

    chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
                JFileChooser chooser = (JFileChooser) evt.getSource();
                File[] oldFiles = (File[]) evt.getOldValue();
                File[] newFiles = (File[]) evt.getNewValue();

                // Get list of selected files
                File[] files = chooser.getSelectedFiles();
            }//from   w  w w  .j  a v  a2 s.c o m
        }
    });
}

From source file:kindleclippings.word.QuizletSync.java

public static void main(String[] args) throws IOException, JSONException, URISyntaxException,
        InterruptedException, BackingStoreException, BadLocationException {

    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new FileNameExtensionFilter("Word documents", "doc", "rtf", "txt"));
    fc.setMultiSelectionEnabled(true);/*  w  w w.  j  av a 2  s  .c  o m*/
    int result = fc.showOpenDialog(null);
    if (result != JFileChooser.APPROVE_OPTION) {
        return;
    }
    File[] clf = fc.getSelectedFiles();
    if (clf == null || clf.length == 0)
        return;

    ProgressMonitor progress = new ProgressMonitor(null, "QuizletSync", "loading notes files", 0, 100);
    progress.setMillisToPopup(0);
    progress.setMillisToDecideToPopup(0);
    progress.setProgress(0);
    try {

        progress.setNote("checking Quizlet account");
        progress.setProgress(5);

        Preferences prefs = kindleclippings.quizlet.QuizletSync.getPrefs();

        QuizletAPI api = new QuizletAPI(prefs.get("access_token", null));

        Collection<TermSet> sets = null;
        try {
            progress.setNote("checking Quizlet library");
            progress.setProgress(10);
            sets = api.getSets(prefs.get("user_id", null));
        } catch (IOException e) {
            if (e.toString().contains("401")) {
                // Not Authorized => Token has been revoked
                kindleclippings.quizlet.QuizletSync.clearPrefs();
                prefs = kindleclippings.quizlet.QuizletSync.getPrefs();
                api = new QuizletAPI(prefs.get("access_token", null));
                sets = api.getSets(prefs.get("user_id", null));
            } else {
                throw e;
            }
        }

        progress.setProgress(15);
        progress.setMaximum(15 + clf.length * 10);
        progress.setNote("uploading new notes");

        int pro = 15;

        int addedSets = 0;
        int updatedTerms = 0;
        int updatedSets = 0;

        for (File f : clf) {
            progress.setProgress(pro);
            List<Clipping> clippings = readClippingsFile(f);

            if (clippings == null) {
                pro += 10;
                continue;
            }

            if (clippings.isEmpty()) {
                pro += 10;
                continue;
            }

            if (clippings.size() < 2) {
                pro += 10;
                continue;
            }

            String book = clippings.get(0).getBook();
            progress.setNote(book);

            TermSet termSet = null;
            String x = book.toLowerCase().replaceAll("\\W", "");

            for (TermSet t : sets) {
                if (t.getTitle().toLowerCase().replaceAll("\\W", "").equals(x)) {
                    termSet = t;
                    break;
                }
            }

            if (termSet == null) {

                addSet(api, book, clippings);
                addedSets++;
                pro += 10;
                continue;
            }

            // compare against existing terms
            boolean hasUpdated = false;
            for (Clipping cl : clippings) {
                if (!kindleclippings.quizlet.QuizletSync.checkExistingTerm(cl, termSet)) {
                    kindleclippings.quizlet.QuizletSync.addTerm(api, termSet, cl);
                    updatedTerms++;
                    hasUpdated = true;
                }
            }

            pro += 10;

            if (hasUpdated)
                updatedSets++;

        }

        if (updatedTerms == 0 && addedSets == 0) {
            JOptionPane.showMessageDialog(null, "Done.\nNo new data was uploaded", "QuizletSync",
                    JOptionPane.OK_OPTION);
        } else {
            if (addedSets > 0) {
                JOptionPane.showMessageDialog(null,
                        String.format("Done.\nCreated %d new sets and added %d cards to %d existing sets",
                                addedSets, updatedSets, updatedTerms),
                        "QuizletSync", JOptionPane.OK_OPTION);
            } else {
                JOptionPane.showMessageDialog(null,
                        String.format("Done.\nAdded %d cards to %d existing sets", updatedTerms, updatedSets),
                        "QuizletSync", JOptionPane.OK_OPTION);
            }
        }
    } finally {
        progress.close();
    }

    System.exit(0);
}

From source file:Main.java

public static void main(String[] argv) {

    JFileChooser chooser = new JFileChooser();

    chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
                JFileChooser chooser = (JFileChooser) evt.getSource();
                File oldFile = (File) evt.getOldValue();
                File newFile = (File) evt.getNewValue();

                System.out.println(oldFile);
                System.out.println(newFile);
                System.out.println(chooser.getSelectedFile());
            } else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
                JFileChooser chooser = (JFileChooser) evt.getSource();
                File[] oldFiles = (File[]) evt.getOldValue();
                File[] newFiles = (File[]) evt.getNewValue();

                Arrays.toString(oldFiles);
                Arrays.toString(newFiles);
                File[] files = chooser.getSelectedFiles();
                Arrays.toString(files);
            }/*from ww  w  .  java 2s . c om*/
        }
    });

    chooser.setVisible(true);

}

From source file:net.sf.vntconverter.VntConverter.java

/**
 * Fhrt den VntConverter mit den angegebnen Optionen aus.
 *//*from  w w  w  .java  2  s  . c  o  m*/
public static void main(String[] args) {
    try {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // Wenn nicht, dann nicht ...
        }
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files (txt) and memo files (vnt)",
                "txt", "vnt");
        fileChooser.setCurrentDirectory(new java.io.File("."));
        fileChooser.setDialogTitle("Choose files to convert ...");
        fileChooser.setFileFilter(filter);
        fileChooser.setMultiSelectionEnabled(true);
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            JFileChooser directoryChooser = new JFileChooser();
            directoryChooser.setCurrentDirectory(new java.io.File("."));
            directoryChooser.setDialogTitle("Choose target directory ...");
            directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            directoryChooser.setAcceptAllFileFilterUsed(false);
            if (directoryChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                VntConverter converter = new VntConverter();
                String targetDirectoy = directoryChooser.getSelectedFile().getAbsolutePath();
                System.out.println(targetDirectoy);
                for (File file : fileChooser.getSelectedFiles()) {
                    if (file.getName().endsWith(".txt")) {
                        converter.encode(file,
                                new File(targetDirectoy + "\\" + file.getName().replace(".txt", ".vnt")));
                    } else if (file.getName().endsWith(".vnt")) {
                        converter.decode(file,
                                new File(targetDirectoy + "\\" + file.getName().replace(".vnt", ".txt")));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception caught", e);
    }
}

From source file:ExcelComponents.FileOpener.java

public static File[] openfiles() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);
    fileChooser.setMultiSelectionEnabled(true);
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File[] selectedFiles = fileChooser.getSelectedFiles().clone();
        return (selectedFiles);
    }//from w w w  .  jav a  2s. com
    return null;

}

From source file:Main.java

/**
 * Consistent way to chosing multiple files to open with JFileChooser.
 * <p>/*w  ww  . ja v  a 2s.  c  o m*/
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File[] based on the user selection can be null.
 */
public static File[] getSystemFiles(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setMultiSelectionEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);
    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFiles();
    }

    return new File[0];
}

From source file:com.mgmtp.jfunk.core.JFunk.java

private static List<File> requestScriptsViaGui() {
    final List<File> scripts = new ArrayList<>();

    try {/* w w w  . j  a v  a  2  s. co m*/
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JFileChooser fileChooser = new JFileChooser(System.getProperty(JFunkConstants.USER_DIR));
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                fileChooser.setMultiSelectionEnabled(true);
                fileChooser.setPreferredSize(new Dimension(800, 450));
                int i = fileChooser.showOpenDialog(null);

                if (i == JFileChooser.APPROVE_OPTION) {
                    File[] files = fileChooser.getSelectedFiles();
                    scripts.addAll(Arrays.asList(files));
                }
            }
        });
    } catch (Exception e) {
        LOG.error("Error while requesting scripts via GUI", e);
    }

    return scripts;
}