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:de.jcup.egradle.other.GroovyParserSourceCollector.java

private void start(String[] args) throws IOException {
    File groovyProjectDirectory;/*w  ww.ja v  a 2s  .  c  om*/
    if (args.length == 0) {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.showOpenDialog(null);
        groovyProjectDirectory = fc.getSelectedFile();
    } else {
        groovyProjectDirectory = new File(args[0]);
    }
    if (groovyProjectDirectory == null) {
        System.err.println("canceled");
        System.exit(1);
    }
    System.out.println("use groovy sources at:" + groovyProjectDirectory.getAbsolutePath());

    assertDirectoryExists(groovyProjectDirectory);
    File groovyTempDirectory = new File(groovyProjectDirectory, "/target/tmp/groovydoc");
    File target = new File("./..//egradle-plugin-main/src/main/java-groovy");
    assertDirectoryExists(target);
    execute(groovyTempDirectory, target);
    System.out.println("DONE");
}

From source file:com.mirth.connect.client.ui.MessageImportDialog.java

private void browseSelected() {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    if (userPreferences != null) {
        File currentDir = new File(userPreferences.get("currentDirectory", ""));

        if (currentDir.exists()) {
            chooser.setCurrentDirectory(currentDir);
        }/*from  w  w w  . j  a v  a2 s .  c om*/
    }

    if (chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) {
        if (userPreferences != null) {
            userPreferences.put("currentDirectory", chooser.getCurrentDirectory().getPath());
        }

        fileTextField.setText(chooser.getSelectedFile().getAbsolutePath());
    }
}

From source file:ch.randelshofer.cubetwister.PreferencesTemplatesPanel.java

private void exportInternalTemplate(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportInternalTemplate
    if (exportFileChooser == null) {
        exportFileChooser = new JFileChooser();
        exportFileChooser.setFileFilter(new ExtensionFileFilter("zip", "Zip Archive"));
        exportFileChooser.setSelectedFile(
                new File(userPrefs.get("cubetwister.exportedHTMLTemplate", "CubeTwister HTML-Template.zip")));
        exportFileChooser.setApproveButtonText(labels.getString("filechooser.export"));
    }/*  ww w  .j a  v a 2s  . com*/
    if (JFileChooser.APPROVE_OPTION == exportFileChooser.showSaveDialog(this)) {
        userPrefs.put("cubetwister.exportedHTMLTemplate", exportFileChooser.getSelectedFile().getPath());
        final File target = exportFileChooser.getSelectedFile();
        new BackgroundTask() {

            @Override
            public void construct() throws IOException {
                if (!target.getParentFile().exists()) {
                    target.getParentFile().mkdirs();
                }
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = getClass().getResourceAsStream("/htmltemplates.tar.bz");
                    out = new FileOutputStream(target);
                    exportTarBZasZip(in, out);
                } finally {
                    try {
                        if (in != null) {
                            in.close();
                        }
                    } finally {
                        if (out != null) {
                            out.close();
                        }
                    }
                }
            }

            private void exportTarBZasZip(InputStream in, OutputStream out) throws IOException {
                ZipOutputStream zout = new ZipOutputStream(out);
                TarInputStream tin = new TarInputStream(new BZip2CompressorInputStream(in));

                for (TarArchiveEntry inEntry = tin.getNextEntry(); inEntry != null;) {
                    ZipEntry outEntry = new ZipEntry(inEntry.getName());
                    zout.putNextEntry(outEntry);
                    if (!inEntry.isDirectory()) {
                        Files.copyStream(tin, zout);
                    }
                    zout.closeEntry();
                }
                zout.finish();
            }
        }.start();
    }
}

From source file:com.sciaps.utils.Util.java

public static void saveCSVFile(StringBuilder strBuilder) {
    JFileChooser chooser = new JFileChooser();

    int retrival = chooser.showSaveDialog(Constants.MAIN_FRAME);
    if (retrival == JFileChooser.APPROVE_OPTION) {

        ProgressStatusPanel progressbar = new ProgressStatusPanel();
        final CustomDialog progressDialog = new CustomDialog(Constants.MAIN_FRAME, "Exporting CSV file",
                progressbar, CustomDialog.NONE_OPTION);
        progressDialog.setSize(400, 100);
        SwingUtilities.invokeLater(new Runnable() {

            @Override//from   w  w  w .  j a  v a 2 s . c om
            public void run() {
                progressDialog.setVisible(true);
            }
        });

        try {
            String fileName = chooser.getSelectedFile().toString();
            if (!fileName.endsWith(".csv") && !fileName.endsWith(".CSV")) {
                fileName = fileName + ".csv";
            }
            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(strBuilder.toString());
            bw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
        }

        progressDialog.dispose();
    }
}

From source file:com.alvermont.javascript.tools.shell.ShellJSConsole.java

public void createFileChooser() {
    this.dlg = new JFileChooser();

    javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            boolean accepted = false;

            if (f.isDirectory()) {
                accepted = true;//from   w ww.jav a2s. c  om
            } else {
                final String name = f.getName();

                if (name.toLowerCase().endsWith(".js")) {
                    accepted = true;
                }
            }

            return accepted;
        }

        public String getDescription() {
            return "JavaScript Files (*.js)";
        }
    };

    this.dlg.addChoosableFileFilter(filter);
}

From source file:com.opendoorlogistics.core.utils.ui.FileBrowserPanel.java

private static JButton createBrowseButton(final boolean directoriesOnly, final String browserApproveButtonText,
        final JTextField textField, final FileFilter... fileFilters) {
    JButton browseButton = new JButton("...");
    browseButton.addActionListener(new ActionListener() {

        @Override/*from   ww w.j  ava  2  s. com*/
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();

            if (textField.getText() != null) {
                File file = new File(textField.getText());

                // if the file doesn't exist but the directory does, get that
                if (!file.exists() && file.getParentFile() != null && file.getParentFile().exists()) {
                    file = file.getParentFile();
                }

                if (!directoriesOnly && file.isFile()) {
                    chooser.setSelectedFile(file);
                }

                if (file.isDirectory() && file.exists()) {
                    chooser.setCurrentDirectory(file);
                }
            }

            if (directoriesOnly) {
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            }

            // add filters and automatically select correct one
            if (fileFilters.length == 1) {
                chooser.setFileFilter(fileFilters[0]);
            } else {
                for (FileFilter filter : fileFilters) {
                    chooser.addChoosableFileFilter(filter);
                    if (filter instanceof FileNameExtensionFilter) {
                        if (matchesFilter((FileNameExtensionFilter) filter, textField.getText())) {
                            chooser.setFileFilter(filter);
                        }
                    }
                }
            }

            if (chooser.showDialog(textField, browserApproveButtonText) == JFileChooser.APPROVE_OPTION) {
                //File selected = processSelectedFile(chooser.getSelectedFile());
                File selected = chooser.getSelectedFile();

                String path = selected.getPath();
                FileFilter filter = chooser.getFileFilter();
                if (filter != null && filter instanceof FileNameExtensionFilter) {

                    boolean found = matchesFilter(((FileNameExtensionFilter) chooser.getFileFilter()), path);

                    if (!found) {
                        String[] exts = ((FileNameExtensionFilter) chooser.getFileFilter()).getExtensions();
                        if (exts.length > 0) {
                            path = FilenameUtils.removeExtension(path);
                            path += "." + exts[0];
                        }
                    }

                }
                textField.setText(path);
            }
        }
    });

    return browseButton;
}

From source file:com.github.rholder.gradle.ui.DependencyViewerStandalone.java

private void promptForGradleBaseDir() {
    JFileChooser c = new JFileChooser();
    c.setDialogTitle(/*from  w  w w  . j a  v  a 2  s.  c o  m*/
            "Pick the top level directory to use when viewing dependencies (in case you have a multi-module project)");
    c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = c.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        gradleBaseDir = c.getSelectedFile().getPath();
    }
}

From source file:com.intuit.tank.tools.debugger.ActionProducer.java

/**
 * //w ww. j av  a2s  .  c o  m
 * @param debuggerFrame
 * @param serviceUrl
 */
public ActionProducer(AgentDebuggerFrame debuggerFrame, String serviceUrl) {
    super();
    this.debuggerFrame = debuggerFrame;
    this.scriptServiceClient = new ScriptServiceClient(serviceUrl);
    this.projectServiceClient = new ProjectServiceClientV1(serviceUrl);
    this.agentServiceClient = new AgentServiceClient(serviceUrl);
    this.dataFileClient = new DataFileClient(serviceUrl);

    jFileChooser = new JFileChooser();
    jFileChooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Agent XML Files";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith("_h.xml");
        }
    });
}

From source file:org.cytoscape.dyn.internal.graphMetrics.GraphMetricsResultsPanel.java

/**
 * /*from w w  w.  j av  a  2s .c  o m*/
 */
public void saveData() {
    JFileChooser saveFileDialog = new JFileChooser();
    int save = saveFileDialog.showSaveDialog(null);
    if (save == JFileChooser.APPROVE_OPTION) {
        FileWriter writer = null;
        try {
            File file = saveFileDialog.getSelectedFile();
            if (file.exists()) {
                if (JOptionPane.showConfirmDialog(null,
                        "The specified file already exists. Do you want to overwrite it?",
                        "Warning - File Exists", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    writer = new FileWriter(file);
                    for (int i = 0; i < this.dataset.getSeriesCount(); i++) {
                        writer.write(this.dataset.getSeries(i).getKey().toString() + "\n");
                        for (int j = 0; j < this.dataset.getSeries(i).getItemCount(); j++) {
                            writer.write(this.dataset.getSeries(i).getDataItem(j).toString() + "\n");
                        }
                    }
                }
            } else {
                writer = new FileWriter(file);
                for (int i = 0; i < this.dataset.getSeriesCount(); i++) {
                    writer.write(this.dataset.getSeries(i).getKey().toString() + "\n");
                    for (int j = 0; j < this.dataset.getSeries(i).getItemCount(); j++) {
                        writer.write(this.dataset.getSeries(i).getDataItem(j).toString() + "\n");
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:csv.to.sql.parser.mainMenu.java

private void btnOpenFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenFileActionPerformed
    // TODO add your handling code here:
    JFileChooser myFile = new JFileChooser();
    myFile.setFileFilter(new FileNameExtensionFilter("CSV Files", "csv"));
    myFile.showOpenDialog(this);
    this.selectedFile = myFile.getSelectedFile();
    if (this.selectedFile.exists()) {
        this.fileSelected = true;
        this.lblFile.setText(this.selectedFile.getName());
        this.btnOpenFile.setEnabled(false);
        this.btnParse.setEnabled(true);
    } else {//  w ww.  j  a v a2 s .co  m
        JOptionPane.showInternalMessageDialog(this, "The file doesn't exists!");
    }

}