Example usage for javax.swing JFileChooser DIRECTORIES_ONLY

List of usage examples for javax.swing JFileChooser DIRECTORIES_ONLY

Introduction

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

Prototype

int DIRECTORIES_ONLY

To view the source code for javax.swing JFileChooser DIRECTORIES_ONLY.

Click Source Link

Document

Instruction to display only directories.

Usage

From source file:bridge.toolkit.ControllerJFrame.java

private void ResourceBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ResourceFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = ResourceFileChooser.showOpenDialog(ControllerJFrame.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = ResourceFileChooser.getSelectedFile();
        ResourceField.setText(file.getAbsolutePath());
    }/*from   ww  w .  ja va  2  s.c  om*/
}

From source file:com.stacksync.desktop.util.FileUtil.java

private static File showBrowseDialogLinux(BrowseType type) {
    LinuxNativeClient nativeClient = LinuxNativeClient.getInstance();

    try {//from ww w  . ja v  a 2 s  . c om
        nativeClient.init("Everything is up to date.");
        Object responseObj = nativeClient.send(new BrowseFileRequest(type));

        // If responseObj returns null means that there is a problem
        // trying to connect with native linux socket.
        if (responseObj == null) {
            //return null;
            // Throw the exception to show the java native browser.
            throw new Exception();
        }

        return (File) responseObj;
    } catch (Exception e) {
        if (type == BrowseType.FILES_ONLY) {
            return showBrowseDialogDefault(JFileChooser.FILES_ONLY);
        } else if (type == BrowseType.DIRECTORIES_ONLY) {
            return showBrowseDialogDefault(JFileChooser.DIRECTORIES_ONLY);
        }

        throw new RuntimeException("Unknown browse type!");
    }
}

From source file:bridge.toolkit.ControllerJFrame.java

private void OutputDirectoryBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {

    OutputFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = OutputFileChooser.showOpenDialog(ControllerJFrame.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = OutputFileChooser.getSelectedFile();
        OutputDirectoryField.setText(file.getAbsolutePath());
    }//from  www  . j  a  v a 2  s  .c  o m
}

From source file:PeerPanel.java

private void saveLocationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveLocationButtonActionPerformed
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(saveDirectory);
    jfc.setMultiSelectionEnabled(false);
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        saveDirectory = jfc.getSelectedFile();
        saveLocationDisplay.setText(saveDirectory.getAbsolutePath());
    }//from   www. j ava2s . c  o  m
}

From source file:net.sf.jabref.gui.FindUnlinkedFilesDialog.java

/**
 * Opens a {@link JFileChooser} and receives the user input as a
 * {@link File} object, which this method returns. <br>
 * <br>/*from   w w w.j  a  v  a 2  s .co m*/
 * The "Open file" dialog will start at the path that is set in the
 * "directory" textfield, or at the last stored path for this dialog, if the
 * textfield is empty. <br>
 * <br>
 * If the user cancels the "Open file" dialog, this method returns null. <br>
 * <br>
 * If the user has selected a valid directory in the "Open file" dialog,
 * this path will be stored persistently for this dialog, so that it can be
 * preset at the next time this dialog is opened.
 *
 * @return The selected directory from the user, or <code>null</code>, if
 *         the user has aborted the selection.
 */
private Path chooseDirectory() {

    if (fileChooser == null) {
        fileChooser = new JFileChooser();
        fileChooser.setAutoscrolls(true);
        fileChooser.setDialogTitle(Localization.lang("Select directory"));
        fileChooser.setApproveButtonText(Localization.lang("Choose directory"));
        fileChooser.setApproveButtonToolTipText(
                Localization.lang("Use the selected directory to start with the search."));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    String path = textfieldDirectoryPath.getText();
    if (path.isEmpty()) {
        fileChooser.setCurrentDirectory(lastSelectedDirectory.toFile());
    } else {
        fileChooser.setCurrentDirectory(Paths.get(path).toFile());
    }

    int result = fileChooser.showOpenDialog(frame);
    if (result == JFileChooser.CANCEL_OPTION) {
        return null;
    }
    Path selectedDirectory = fileChooser.getSelectedFile().toPath();
    String filepath = "";
    if (selectedDirectory != null) {
        filepath = selectedDirectory.toAbsolutePath().toString();
    }
    textfieldDirectoryPath.setText(filepath);

    return selectedDirectory;
}

From source file:archive_v1.Archive_Form.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    CreateConnection();//from  w  ww .j a  v a 2 s.  c  om
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int a = jfc.showOpenDialog(this);
    if (a == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        try {
            // What to do with the file, e.g. display it in a TextArea
            System.out.println(file.getAbsolutePath());
        } catch (Exception ex) {
            System.out.println("problem accessing file" + file.getAbsolutePath());
        }
        File[] folder = new File(file.getAbsolutePath()).listFiles();
        for (File vid : folder) {
            filename = vid.getName();
            size = getFileSize(file.getAbsolutePath() + "\\" + filename);
            tape_id = generateTapeId();
            insertArchive(filename, size, tape_id, username);

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:cn.labthink.ReadAccess060.java

private void jButton_exportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_exportActionPerformed
    JFileChooser jfc = new JFileChooser();
    ExtensionFileFilter filter;//from  w  w w  .j a va  2  s . c  om

    if (jTable1.getSelectedRowCount() == 1) {
        // filter
        filter = new ExtensionFileFilter("xls", false, true);
        filter.setDescription("Save Export File");

        jfc.setDialogTitle("Create the Export Excel file");
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    } else if (jTable1.getSelectedRowCount() > 1) {
        // filter
        filter = new ExtensionFileFilter("", false, true);
        filter.setDescription("Save Export Files");
        jfc.setDialogTitle("Choose the Export Directory");
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    } else {
        //?
        jLabel_info.setText("<html><font color='red'>No Record Selected</font></html>");
        return;
    }

    //?
    FileSystemView fsv = FileSystemView.getFileSystemView();
    //?
    jfc.setCurrentDirectory(fsv.getHomeDirectory());

    jfc.setMultiSelectionEnabled(false);
    jfc.setDialogType(JFileChooser.SAVE_DIALOG);

    jfc.setFileFilter(filter);
    int result = jfc.showSaveDialog(this); // ""?

    if (result == JFileChooser.APPROVE_OPTION) {
        if (jTable1.getSelectedRowCount() == 1) {
            //
            String filesrc = jfc.getSelectedFile().getAbsolutePath();
            if (!filesrc.toLowerCase().endsWith(".xls")) {
                filesrc = jfc.getSelectedFile().getAbsolutePath() + ".xls";
            }
            outputfile = new File(filesrc);
            jLabel_info.setText("Exported File:" + outputfile.getAbsolutePath());
        } else if (jTable1.getSelectedRowCount() > 1) {
            //
            outputfile = jfc.getSelectedFile().isDirectory() ? jfc.getSelectedFile()
                    : jfc.getSelectedFile().getParentFile();
            if (outputfile == null) {
                outputfile = fsv.getHomeDirectory();
            }
            jLabel_info.setText("Exported to path:" + outputfile.getAbsolutePath());
        } else {
            //?
            return;
        }

    } else {
        return;
    }

    if (inputfile == null) {
        return;
    }

    int[] rows = jTable1.getSelectedRows();
    if (rows.length == 1) {
        //?
        book = null;
        ExportOneRecord(rows[0]);
    } else {
        File path = outputfile;

        for (int rowindex = 0; rowindex < rows.length; rowindex++) {
            int k = rows[rowindex];
            book = null;
            outputfile = new File(path.getAbsolutePath() + "/" + jTable1.getValueAt(k, 0) + ".xls");
            ExportOneRecord(k);
        }
    }
    //        int k = jTable1.getSelectedRow();
    //        ExportOneRecord(k);

}

From source file:de.evaluationtool.gui.EvaluationFrameActionListener.java

private void saveReference(boolean mustSupportEvaluation, boolean includeEvaluation)
        throws FileNotFoundException {
    Set<ReferenceFormat> formats = mustSupportEvaluation
            ? ReferenceFormats.REFERENCE_FORMATS.evaluationIncludingFormats
            : ReferenceFormats.REFERENCE_FORMATS.formats;
    formats.retainAll(ReferenceFormats.REFERENCE_FORMATS.writeableFormats);

    ReferenceFormat format = formatChooser(formats);
    if (format == null) {
        return;/* www  . j ava2s. co m*/
    }

    JFileChooser chooser = new JFileChooser("Save reference. Please choose a file.");
    chooser.setCurrentDirectory(frame.defaultDirectory);
    chooser.setFileSelectionMode(
            format.readsDirectory() ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY);
    if (format.getFileExtension() != null) {
        chooser.addChoosableFileFilter(
                new FilesystemFilter(format.getFileExtension(), format.getDescription()));
    } else {
        chooser.setAcceptAllFileFilterUsed(true);
    }

    int returnVal = chooser.showSaveDialog(frame);
    if (returnVal != JFileChooser.APPROVE_OPTION)
        return;
    System.out.print("Saving...");
    format.writeReference(frame.reference, chooser.getSelectedFile(), includeEvaluation);
    //frame.loadPositiveNegativeNT(chooser.getSelectedFile());
    System.out.println("saving finished.");
}

From source file:br.com.bgslibrary.gui.MainFrame.java

private String getFilePath(boolean onlyDir) {
    final JFileChooser fc = new JFileChooser(basePath);

    if (onlyDir)//from  www  . java  2  s  .c  om
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnVal = fc.showOpenDialog(this);

    String relative = "";

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        String path = file.getAbsolutePath();
        String base = basePath;
        relative = new File(base).toURI().relativize(new File(path).toURI()).getPath();

        System.out.println("Opening: " + relative);
    } else
        System.out.println("Open command cancelled by user");

    return relative;
}

From source file:org.fhaes.jsea.JSEAFrame.java

/**
 * Initialize the menu/toolbar actions./* w  ww. j av  a  2 s  .  c o  m*/
 */
private void initActions() {

    final JFrame glue = this;

    actionFileExit = new FHAESAction("Close", "close.png") { //$NON-NLS-1$

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            dispose();
        }
    };

    actionChartProperties = new FHAESAction("Chart properties", "properties.png") { //$NON-NLS-1$

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            ChartEditor editor = ChartEditorManager
                    .getChartEditor(jsea.getChartList().get(segmentComboBox.getSelectedIndex()).getChart());
            int result = JOptionPane.showConfirmDialog(glue, editor, "Properties", JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                editor.updateChart(jsea.getChartList().get(segmentComboBox.getSelectedIndex()).getChart());
            }
        }
    };

    actionReset = new FHAESAction("Reset", "filenew.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            Object[] options = { "Yes", "No", "Cancel" };
            int n = JOptionPane.showOptionDialog(glue, "Are you sure you want to start a new analysis?",
                    "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                    options[2]);

            if (n == JOptionPane.YES_OPTION) {
                setToDefault();
            }
        }
    };

    actionRun = new FHAESAction("Run analysis", "run.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            runAnalysis();
        }
    };

    actionSaveAll = new FHAESAction("Save all results", "save_all.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            if (jsea == null)
                return;

            File file;
            JFileChooser fc;

            // Open file chooser in last folder if possible
            if (App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null) != null) {
                fc = new JFileChooser(App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null));
            } else {
                fc = new JFileChooser();
            }

            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            // Show dialog and get specified file
            int returnVal = fc.showSaveDialog(glue);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, file.getAbsolutePath());
            } else {
                return;
            }

            File f;
            try {
                f = new File(file.getAbsolutePath() + File.separator + "report.txt");
                saveReportTXT(f);

                f = new File(file.getAbsolutePath() + File.separator + "report.pdf");
                saveReportPDF(f);

                f = new File(file.getAbsolutePath() + File.separator + "chart.png");
                saveChartPNG(f);

                f = new File(file.getAbsolutePath() + File.separator + "chart.pdf");
                saveChartPDF(f);

                f = new File(file.getAbsolutePath() + File.separator + "data.xls");
                saveDataXLS(f);

                f = new File(file.getAbsolutePath());
                saveDataCSV(f);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    actionSaveData = new FHAESAction("Save data tables", "table.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            if (jsea == null)
                return;

            File file;
            JFileChooser fc;

            // Open file chooser in last folder if possible
            if (App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null) != null) {
                fc = new JFileChooser(App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null));
            } else {
                fc = new JFileChooser();
            }

            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            // Show dialog and get specified file
            int returnVal = fc.showSaveDialog(glue);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, file.getAbsolutePath());
            } else {
                return;
            }

            try {
                saveDataCSV(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    actionSaveReport = new FHAESAction("Save report", "report.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            if (jsea == null)
                return;

            File file;
            JFileChooser fc;

            // Open file chooser in last folder if possible
            if (App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null) != null) {
                fc = new JFileChooser(App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null));
            } else {
                fc = new JFileChooser();
            }

            // Set file filters
            fc.setAcceptAllFileFilterUsed(false);
            TXTFileFilter txtfilter = new TXTFileFilter();
            PDFFilter pdffilter = new PDFFilter();
            fc.addChoosableFileFilter(txtfilter);
            fc.addChoosableFileFilter(pdffilter);
            fc.setFileFilter(txtfilter);
            FileFilter chosenFilter;

            // Show dialog and get specified file
            int returnVal = fc.showSaveDialog(glue);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                chosenFilter = fc.getFileFilter();
                App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, file.getAbsolutePath());
            } else {
                return;
            }

            // Handle file type and extensions nicely
            if (FilenameUtils.getExtension(file.getAbsolutePath()).equals("")) {
                if (chosenFilter.equals(txtfilter)) {
                    file = new File(file.getAbsoluteFile() + ".txt");
                } else if (chosenFilter.equals(pdffilter)) {
                    file = new File(file.getAbsoluteFile() + ".pdf");
                }
            } else if (FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase().equals("txt")
                    && chosenFilter.equals("pdf")) {
                chosenFilter = txtfilter;
            } else if (FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase().equals("pdf")
                    && chosenFilter.equals("txt")) {
                chosenFilter = pdffilter;
            }

            // If file already exists confirm overwrite
            if (file.exists()) {
                // Check we have write access to this file
                if (!file.canWrite()) {
                    JOptionPane.showMessageDialog(glue, "You do not have write permission to this file",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                int n = JOptionPane.showConfirmDialog(glue,
                        "File: " + file.getName() + " already exists. " + "Would you like to overwrite it?",
                        "Overwrite file?", JOptionPane.YES_NO_OPTION);
                if (n != JOptionPane.YES_OPTION) {
                    return;
                }
            }

            // Do save
            try {

                if (chosenFilter.equals(txtfilter)) {
                    saveReportTXT(file);
                } else if (chosenFilter.equals(pdffilter)) {
                    saveReportPDF(file);
                } else {
                    log.error("No export file format chosen.  Shouldn't be able to get here!");
                }
            } catch (IOException e) {
                JOptionPane.showMessageDialog(glue, "Unable to save report.  Check log file.", "Warning",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }

        }
    };

    actionSaveChart = new FHAESAction("Save chart", "barchart.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            if (jsea == null)
                return;

            File file;
            JFileChooser fc;

            // Open file chooser in last folder if possible
            if (App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null) != null) {
                fc = new JFileChooser(App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null));
            } else {
                fc = new JFileChooser();
            }

            // Set file filters
            fc.setAcceptAllFileFilterUsed(false);
            PNGFilter pngfilter = new PNGFilter();
            PDFFilter pdffilter = new PDFFilter();
            fc.addChoosableFileFilter(pngfilter);
            fc.addChoosableFileFilter(pdffilter);
            fc.setFileFilter(pngfilter);
            FileFilter chosenFilter;

            // Show dialog and get specified file
            int returnVal = fc.showSaveDialog(glue);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                chosenFilter = fc.getFileFilter();
                App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, file.getAbsolutePath());
            } else {
                return;
            }

            // Handle file type and extensions nicely
            if (FilenameUtils.getExtension(file.getAbsolutePath()).equals("")) {
                if (chosenFilter.equals(pngfilter)) {
                    file = new File(file.getAbsoluteFile() + ".png");
                } else if (chosenFilter.equals(pdffilter)) {
                    file = new File(file.getAbsoluteFile() + ".pdf");
                }
            } else if (FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase().equals("png")
                    && chosenFilter.equals("pdf")) {
                chosenFilter = pngfilter;
            } else if (FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase().equals("pdf")
                    && chosenFilter.equals("png")) {
                chosenFilter = pdffilter;
            }

            // If file already exists confirm overwrite
            if (file.exists()) {
                // Check we have write access to this file
                if (!file.canWrite()) {
                    JOptionPane.showMessageDialog(glue, "You do not have write permission to this file",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                int n = JOptionPane.showConfirmDialog(glue,
                        "File: " + file.getName() + " already exists. " + "Would you like to overwrite it?",
                        "Overwrite file?", JOptionPane.YES_NO_OPTION);
                if (n != JOptionPane.YES_OPTION) {
                    return;
                }
            }

            // Do save
            try {

                if (chosenFilter.equals(pngfilter)) {
                    saveChartPNG(file);

                } else if (chosenFilter.equals(pdffilter)) {

                    saveChartPDF(file);
                } else {
                    log.error("No export file format chosen.  Shouldn't be able to get here!");
                }
            } catch (IOException e) {
                JOptionPane.showMessageDialog(glue, "Unable to save chart.  Check log file.", "Warning",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }

        }
    };

    actionCopy = new FHAESAction("Copy", "edit_copy.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            copyCurrentSelectionToClipboard();
        }
    };

    actionLagMap = new FHAESAction("LagMap", "lagmap22.png") {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent event) {

            launchLagMap();
        }
    };

}