Example usage for javax.swing JFileChooser JFileChooser

List of usage examples for javax.swing JFileChooser JFileChooser

Introduction

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

Prototype

public JFileChooser() 

Source Link

Document

Constructs a JFileChooser pointing to the user's default directory.

Usage

From source file:com.novadart.silencedetect.SilenceDetect.java

public static void main(String[] args) {

    // create the parser
    CommandLineParser parser = new BasicParser();
    try {/*w w  w  . j a v a  2  s. c  o m*/
        // parse the command line arguments
        CommandLine line = parser.parse(OPTIONS, args);

        if (line.hasOption("h")) {

            printHelp();

        } else {

            String decibels = "-10";
            if (line.hasOption("b")) {
                decibels = "-" + line.getOptionValue("b");
            } else {
                throw new RuntimeException();
            }

            String videoFile = null;

            if (line.hasOption("i")) {

                videoFile = line.getOptionValue("i");

                Boolean debug = line.hasOption("d");

                if (line.hasOption("t")) {

                    System.out.println(printAudioTracksList(videoFile, debug));

                } else if (line.hasOption("s")) {

                    int trackNumber = Integer.parseInt(line.getOptionValue("s"));
                    System.out.println(printAudioTrackSilenceDuration(videoFile, trackNumber, decibels, debug));

                } else {

                    printHelp();

                }

            } else if (line.hasOption("-j")) {

                // choose file
                final JFileChooser fc = new JFileChooser();
                fc.setVisible(true);
                int returnVal = fc.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {

                    videoFile = fc.getSelectedFile().getAbsolutePath();

                } else {
                    return;
                }

                JTextArea tracks = new JTextArea();
                tracks.setText(printAudioTracksList(videoFile, true));
                JSpinner trackNumber = new JSpinner();
                trackNumber.setValue(1);
                final JComponent[] inputs = new JComponent[] { new JLabel("Audio Tracks"), tracks,
                        new JLabel("Track to analyze"), trackNumber };
                JOptionPane.showMessageDialog(null, inputs, "Select Audio Track", JOptionPane.PLAIN_MESSAGE);

                JTextArea results = new JTextArea();
                results.setText(printAudioTrackSilenceDuration(videoFile, (int) trackNumber.getValue(),
                        decibels, false));
                final JComponent[] resultsInputs = new JComponent[] { new JLabel("Results"), results };
                JOptionPane.showMessageDialog(null, resultsInputs, "RESULTS!", JOptionPane.PLAIN_MESSAGE);

            } else {
                printHelp();
                return;
            }

        }

    } catch (ParseException | IOException | InterruptedException exp) {
        // oops, something went wrong
        System.out.println("There was a problem :(\nReason: " + exp.getMessage());
    }
}

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);//from  www.  jav a  2s.  c om
    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:FileTransferHandler.java

/**
 * This class demonstrates the FileTransferHandler by installing it on a
 * JTextArea component and providing a JFileChooser to drag and cut files.
 *//*from  ww  w.ja v  a2  s  .co  m*/

public static void main(String[] args) {
    // Here's the text area. Note how we wrap our TransferHandler
    // around the default handler returned by getTransferHandler()
    JTextArea textarea = new JTextArea();
    TransferHandler defaultHandler = textarea.getTransferHandler();
    textarea.setTransferHandler(new FileTransferHandler(defaultHandler));
    // Here's a JFileChooser, with dragging explicitly enabled.
    JFileChooser filechooser = new JFileChooser();
    filechooser.setDragEnabled(true);

    // Display them both in a window
    JFrame f = new JFrame("File Transfer Handler Test");
    f.getContentPane().add(new JScrollPane(textarea), "Center");
    f.getContentPane().add(filechooser, "South");
    f.setSize(400, 600);
    f.setVisible(true);
}

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

/**
 * Fhrt den VntConverter mit den angegebnen Optionen aus.
 *///from  ww w.  jav a2  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:Main.java

public static File promptForImageFile(JPanel parent) {
    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(parent);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
        return chooser.getSelectedFile();
    }//from www  .j  a  v a  2 s .  com
    return null;
}

From source file:Main.java

public static JFileChooser makeFileChooser() {
    final JFileChooser temp = new JFileChooser();
    final String dir = System.getProperty("user.dir");
    final File fileDir = new File(dir);
    temp.setCurrentDirectory(fileDir);//w w w  .  j  a  va 2  s  . c o m
    return temp;
}

From source file:Main.java

public static File chooseDir(Component parent, String title) {
    JFileChooser chooser = new JFileChooser();
    // chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(title);/*from w  w  w .j a  v  a 2  s. c om*/
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonText("Select Dir");
    return chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null;
}

From source file:Main.java

public static File chooseFile(Component parentComponent, String title) {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    jfc.showDialog(parentComponent, title);
    return jfc.getSelectedFile();
}

From source file:Main.java

public static File chooseDirectory(Component parentComponent, String title) {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.showDialog(parentComponent, title);
    return jfc.getSelectedFile();
}

From source file:Main.java

public static File saveImageFile(Component parent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {
        @Override//from   w  ww . j a v a2s. c o m
        public String getDescription() {
            return "bmp";
        }

        @Override
        public boolean accept(File f) {
            String name = f.getName();
            boolean accepted = f.isDirectory() || name.endsWith(".bmp");
            return accepted;
        }
    });

    fileChooser.showSaveDialog(parent);
    return fileChooser.getSelectedFile();
}