Example usage for javax.swing JFileChooser showSaveDialog

List of usage examples for javax.swing JFileChooser showSaveDialog

Introduction

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

Prototype

public int showSaveDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up a "Save File" file chooser dialog.

Usage

From source file:org.gephi.ui.components.ReportSelection.java

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
    final String html = this.mHTMLReport;

    final String path = NbPreferences.forModule(SimpleHTMLReport.class).get(LAST_PATH, null);
    JFileChooser fileChooser = new JFileChooser(path);
    //fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fileChooser.showSaveDialog(WindowManager.getDefault().getMainWindow());
    if (result == JFileChooser.APPROVE_OPTION) {
        final File destinationFolder = fileChooser.getSelectedFile();
        NbPreferences.forModule(SimpleHTMLReport.class).put(LAST_PATH, destinationFolder.getAbsolutePath());
        Thread saveReportThread = new Thread(new Runnable() {

            @Override//ww w  .  j  av a  2s  .c o m
            public void run() {
                try {
                    if (saveReport(html, destinationFolder)) {
                        StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(SimpleHTMLReport.class,
                                "SimpleHTMLReport.status.saveSuccess", destinationFolder.getName()));
                    } else {
                        StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(SimpleHTMLReport.class,
                                "SimpleHTMLReport.status.saveError", destinationFolder.getName()));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, "SaveReportTask");
        saveReportThread.start();

    }
}

From source file:org.giswater.controller.MenuController.java

private String chooseSqlFile(boolean save) {

    String path = "";
    JFileChooser chooser = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("SQL extension file", "sql");
    chooser.setFileFilter(filter);//from ww w.jav  a 2 s .co m
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle(Utils.getBundleString("MenuController.file_sql"));
    File file = new File(PropertiesDao.getLastSqlPath());
    chooser.setCurrentDirectory(file.getParentFile());
    int returnVal;
    if (save) {
        returnVal = chooser.showSaveDialog(mainFrame);
    } else {
        returnVal = chooser.showOpenDialog(mainFrame);
    }
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File fileSql = chooser.getSelectedFile();
        path = fileSql.getAbsolutePath();
        if (path.lastIndexOf(".") == -1) {
            path += ".sql";
            fileSql = new File(path);
        }
        PropertiesDao.setLastSqlPath(path);
    }
    return path;

}

From source file:org.giswater.controller.MenuController.java

private File gswChooseFile(boolean save) {

    String path = "";
    File file = null;//from w  w  w.  j av a2  s . c om
    JFileChooser chooser = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("GSW extension file", "gsw");
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle(Utils.getBundleString("MenuController.gsw_file"));
    File fileProp = new File(prop.get("FILE_GSW", Utils.getLogFolder()));
    chooser.setCurrentDirectory(fileProp.getParentFile());
    int returnVal;
    if (save) {
        returnVal = chooser.showSaveDialog(mainFrame);
    } else {
        returnVal = chooser.showOpenDialog(mainFrame);
    }
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        path = file.getAbsolutePath();
        if (path.lastIndexOf(".") == -1) {
            path += ".gsw";
            file = new File(path);
        }
    }

    return file;

}

From source file:org.javascool.polyfilewriter.Gateway.java

/**
 * Open an AWT FileDialog.//from  www  . j a va 2s . co m
 * Allow to ask to the user to ask any file to the user.
 *
 * @param forSave If it's true, the AWT Dialog will be opened in save mode. Otherwise, it will be opened in open
 *                mode
 * @param ext     Allowed extensions separated by spaces. Leave null or empty string to allow all extensions
 * @return The path of the selected file.
 */
public String askFile(final boolean forSave, String ext, final String path) throws Exception {
    assertSafeUsage();
    try {
        if (ext == null)
            ext = "";
        try {
            final JFileChooser fc = AccessController.doPrivileged(new PrivilegedAction<JFileChooser>() {
                public JFileChooser run() {
                    return new JFileChooser(getFile(path));
                }
            });
            return AccessController.doPrivileged(new PrivilegedAction<String>() {
                @Override
                public String run() {
                    int r = JFileChooser.ABORT;
                    if (forSave)
                        r = fc.showSaveDialog(null);
                    else
                        r = fc.showOpenDialog(null);
                    if (r == JFileChooser.APPROVE_OPTION) {
                        final File file = fc.getSelectedFile();
                        return file.getAbsolutePath();
                    } else
                        return null;
                }
            });
        } catch (Exception e) {
            return null;
        }
    } catch (Exception e) {
        popException(e);
        throw e;
    }
}

From source file:org.javaswift.cloudie.CloudiePanel.java

protected void onDownloadStoredObject() {
    Container container = getSelectedContainer();
    List<StoredObject> obj = getSelectedStoredObjects();
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(lastFolder);
    if (obj.size() == 1) {
        chooser.setSelectedFile(new File(obj.get(0).getName()));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    } else {//w  w w.  ja va2 s  .  co  m
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }
    if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File selected = chooser.getSelectedFile();
        for (StoredObject so : obj) {
            File target = selected;
            if (target.isDirectory()) {
                target = new File(selected, so.getName());
            }
            if (target.exists()) {
                if (confirm("File '" + target.getName() + "' already exists. Overwrite?")) {
                    doSaveStoredObject(target, container, so);
                }
            } else {
                doSaveStoredObject(target, container, so);
            }
        }
        lastFolder = selected.isFile() ? selected.getParentFile() : selected;
    }

}

From source file:org.jcurl.core.jnlp.FileDialogSwing.java

public Contents saveAsFileDialog(final String pathHint, final String[] extensions, final Contents contents,
        final Component parent) {
    final JFileChooser fc = createFileChooser(pathHint, extensions, false);
    final int ret = fc.showSaveDialog(parent);
    switch (ret) {
    case 0:/*from  w ww . j a v  a  2  s. c  o m*/
        throw new NotImplementedYetException();
    case 1:
        return null;
    default:
        log.warn("Ignored Dialog Result " + ret);
        return null;
    }
}

From source file:org.jcurl.core.jnlp.FileDialogSwing.java

public Contents saveFileDialog(final String pathHint, final String[] extensions, final InputStream stream,
        final String name, final Component parent) {
    final JFileChooser fc = createFileChooser(pathHint, extensions, true);
    final int ret = fc.showSaveDialog(parent);
    switch (ret) {
    case 0:/*from   w  w w  .  ja va 2s  .  c  o  m*/
        try {
            final Contents c = new ContentsFile(fc.getSelectedFile());
            final FileOutputStream fo = new FileOutputStream(fc.getSelectedFile());
            // TODO fo.write(arg0)
            fo.close();
            return c;
        } catch (final IOException e) {
            throw new RuntimeException("Unhandled", e);
        }
    case 1:
        return null;
    default:
        log.warn("Ignored Dialog Result " + ret);
        return null;
    }
}

From source file:org.jcurl.demo.editor.EditorApp.java

boolean chooseSaveFile(final File def) {
    final JFileChooser fc = new JcxFileChooser(def);
    if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this)) {
        setCurrentFile(fc.getSelectedFile());
        return true;
    }/*from   ww  w .  j  av a  2  s. c o m*/
    return false;
}

From source file:org.jcurl.demo.editor.EditorApp.java

void cmdExportPng() throws IOException {
    log.debug("-");
    final JFileChooser fc = new PngFileChooser(currentFile);
    if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this)) {
        final File dst = fc.getSelectedFile();
        try {//from w  ww .  ja  v  a 2 s  .c  o m
            this.setCursor(Cwait);
            master.exportPng(dst);
        } finally {
            this.setCursor(Cdefault);
        }
    }
}

From source file:org.jcurl.demo.tactics.JCurlShotPlanner.java

/**
 * Render the current view into a <a/*from   w ww .j av a2s.co  m*/
 * href="http://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</a>
 * image (File Menu Action).
 * 
 * @see ImageIO#write(java.awt.image.RenderedImage, String, File)
 */
@Action(block = BlockingScope.ACTION)
public Task<Void, Void> fileExportPng() {
    final JFileChooser fcPng = createPngChooser(getFile(), "exportPngFileChooser");
    for (;;) {
        if (JFileChooser.APPROVE_OPTION != fcPng.showSaveDialog(getMainFrame()))
            return null;
        final File dst = gui.ensureSuffix(fcPng.getSelectedFile(), pngPat);
        if (!askOverwrite(dst))
            continue;

        return new WaitCursorTask<Void, Void>(this) {
            @Override
            protected Void doCursor() throws Exception {
                renderPng(tactics, dst);
                return null;
            }
        };
    }
}