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:com.holycityaudio.SpinCAD.SpinCADFile.java

public void fileSaveAsm(SpinCADPatch patch) {
    // Create a file chooser
    String savedPath = prefs.get("MRUSpnFolder", "");

    final JFileChooser fc = new JFileChooser(savedPath);
    // In response to a button click:
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Spin ASM Files", "spn");
    fc.setFileFilter(filter);/* w ww  .  j a  v a 2  s.c om*/
    // XXX DEBUG
    fc.showSaveDialog(new JFrame());
    File fileToBeSaved = fc.getSelectedFile();

    if (!fc.getSelectedFile().getAbsolutePath().endsWith(".spn")) {
        fileToBeSaved = new File(fc.getSelectedFile() + ".spn");
    }
    int n = JOptionPane.YES_OPTION;
    if (fileToBeSaved.exists()) {
        JFrame frame1 = new JFrame();
        n = JOptionPane.showConfirmDialog(frame1, "Would you like to overwrite it?", "File already exists!",
                JOptionPane.YES_NO_OPTION);
    }
    if (n == JOptionPane.YES_OPTION) {
        String filePath = fileToBeSaved.getPath();
        fileToBeSaved.delete();

        try {
            fileSaveAsm(patch, filePath);
        } catch (IOException e) {
            JOptionPane.showOptionDialog(null, "File save error!", "Error", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            e.printStackTrace();
        }
        saveMRUSpnFolder(filePath);
    }
}

From source file:controller.CCInstance.java

private String userLoadLibraryPKCS11() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle(Bundle.getBundle().getString("openLibrary"));
    int userSelection = fileChooser.showSaveDialog(null);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        String dest = fileChooser.getSelectedFile().getAbsolutePath();
        File file = new File(dest);
        if (file.exists()) {
            return dest;
        }//from  ww  w.j  a  v a2  s  .com
    }
    return null;
}

From source file:com.holycityaudio.SpinCAD.SpinCADFile.java

public void fileSavePatchAs(SpinCADPatch p) {
    // Create a file chooser
    String savedPath = prefs.get("MRUPatchFolder", "");
    final JFileChooser fc = new JFileChooser(savedPath);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("SpinCAD Files", "spcd");
    fc.setFileFilter(filter);//from w  ww. j av a2  s .  co m
    fc.setSelectedFile(new File(p.patchFileName));
    int returnVal = fc.showSaveDialog(new JFrame());
    // need to process user canceling box right here
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        // In response to a button click:
        File fileToBeSaved = fc.getSelectedFile();

        if (!fileToBeSaved.getAbsolutePath().endsWith(".spcd")) {
            fileToBeSaved = new File(fc.getSelectedFile() + ".spcd");
        }
        int n = JOptionPane.YES_OPTION;
        if (fileToBeSaved.exists()) {
            JFrame frame = new JFrame();
            n = JOptionPane.showConfirmDialog(frame, "Would you like to overwrite it?", "File already exists!",
                    JOptionPane.YES_NO_OPTION);
        }
        if (n == JOptionPane.YES_OPTION) {
            try {
                String filePath = fileToBeSaved.getPath();
                String fileName = fileToBeSaved.getName();
                p.patchFileName = fileName;
                fileSavePatch(p);
                recentPatchFileList.add(fileToBeSaved);
                saveMRUPatchFolder(filePath);

            } catch (Exception e) { // thrown over in SpinCADFile.java
                e.printStackTrace();
                SpinCADDialogs.MessageBox("File save failed!", "look at stack trace for info");
            } finally {
                p.setChanged(false);
            }
        }
    }
}

From source file:com.holycityaudio.SpinCAD.SpinCADFile.java

public void fileSaveBankAs(SpinCADBank b) {
    // Create a file chooser
    String savedPath = prefs.get("MRUBankFolder", "");
    final JFileChooser fc = new JFileChooser(savedPath);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("SpinCAD Bank Files", "spbk");
    fc.setFileFilter(filter);/*  ww  w . ja va  2s .  c o m*/
    fc.setSelectedFile(new File(b.bankFileName));
    int returnVal = fc.showSaveDialog(new JFrame());
    // need to process user canceling box right here
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        // In response to a button click:
        File fileToBeSaved = fc.getSelectedFile();

        if (!fc.getSelectedFile().getAbsolutePath().endsWith(".spbk")) {
            fileToBeSaved = new File(fc.getSelectedFile() + ".spbk");
        }
        b.bankFileName = fileToBeSaved.getName();

        int n = JOptionPane.YES_OPTION;
        if (fileToBeSaved.exists()) {
            JFrame frame = new JFrame();
            n = JOptionPane.showConfirmDialog(frame, "Would you like to overwrite it?", "File already exists!",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                try {
                    fileSaveBank(b);
                } finally {
                }
            }
        } else {
            fileSaveBank(b);
        }
        b.changed = false;
        recentBankFileList.add(fileToBeSaved);
        saveMRUBankFolder(fileToBeSaved.getPath());
        b.bankFileName = fileToBeSaved.getName();
    }
}

From source file:au.com.jwatmuff.eventmanager.gui.main.LoadCompetitionWindow.java

private void saveBackupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBackupButtonActionPerformed
    DatabaseInfo info = (DatabaseInfo) competitionList.getSelectedValue();
    if (info == null || !info.local)
        return;/*w ww  . j  a  va 2 s.  c o m*/
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new FileNameExtensionFilter("Event Manager Files", "evm"));
    if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        if (!file.getName().toLowerCase().endsWith(".evm"))
            file = new File(file.getAbsolutePath() + ".evm");
        if (file.exists()) {
            int result = JOptionPane.showConfirmDialog(rootPane,
                    file.getName() + " already exists. Overwrite file?", "Save Backup",
                    JOptionPane.YES_NO_OPTION);
            if (result != JOptionPane.YES_OPTION)
                return;
        }
        try {
            File tempDir = Files.createTempDirectory("event-manager").toFile();
            FileUtils.copyDirectory(info.localDirectory, tempDir);

            File lockFile = new File(tempDir, "update.dat.lock");
            lockFile.delete();

            /* change id */
            Properties props = new Properties();
            FileReader fr = new FileReader(new File(tempDir, "info.dat"));
            props.load(fr);
            fr.close();
            props.setProperty("old-UUID", props.getProperty("UUID", "none"));
            props.setProperty("UUID", UUID.randomUUID().toString());
            FileWriter fw = new FileWriter(new File(tempDir, "info.dat"));
            props.store(fw, "");
            fw.close();

            ZipUtils.zipFolder(tempDir, file, false);
        } catch (Exception e) {
            GUIUtils.displayError(this, "Failed to save file: " + e.getMessage());
        }
    }
}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);/*from  w  w w.  j ava2 s .c o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                File[] sf = chooser.getSelectedFiles();
                String filelist = "nothing";
                if (sf.length > 0)
                    filelist = sf[0].getName();
                for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                }
                statusbar.setText("You chose " + filelist);
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You saved "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You opened "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    c.add(openButton);
    c.add(saveButton);
    c.add(dirButton);
    c.add(statusbar);
}

From source file:dylemator.UserResultList.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
    if (this.filenameCombo.getSelectedIndex() == 0)
        return;/*  w w w  .  j av a2s.c om*/
    String sheetName = (String) this.filenameCombo.getSelectedItem();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet(sheetName);
    Row headerRow = sheet.createRow(0);
    String[] headers = exportData.get(0);
    int numOfColumns = headers.length;
    for (int i = 0, j = 0; i < numOfColumns; i++) {
        if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
            continue;
        Cell cell = headerRow.createCell(j++);
        cell.setCellValue(headers[i]);
    }

    int rowCount = exportData.size();
    for (int rownum = 1; rownum < rowCount; rownum++) {
        Row row = sheet.createRow(rownum);
        String[] values = exportData.get(rownum);
        for (int i = 0, j = 0; i < numOfColumns; i++) {
            if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
                continue;
            Cell cell = row.createCell(j++);
            cell.setCellValue(values[i]);
        }
    }

    String defaultFilename = "Export.xlsx";
    JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
    f.setSelectedFile(new File(defaultFilename));
    f.setDialogTitle("Wybierz nazw dla pliku eksportu");
    f.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.getName().endsWith(".xlsx"))
                return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "";
        }
    };
    f.setFileFilter(ff);

    File file = null;
    int save = f.showSaveDialog(this);
    if (save == JFileChooser.APPROVE_OPTION)
        file = f.getSelectedFile();
    else
        return;

    FileOutputStream out;
    try {
        out = new FileOutputStream(file);
        wb.write(out);
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:SciTK.Plot.java

/** 
* Save data for generic plot to file //from   w  w  w.ja  v a 2s  .com
*/
public void saveData() {
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new ExtensionFileFilter("CSV", new String[] { "csv,CSV" }));
    // remove default option:
    fc.setAcceptAllFileFilterUsed(false);

    // Select a file using the JFileChooser dialog:
    int fc_return = fc.showSaveDialog(this);

    if (fc_return == JFileChooser.APPROVE_OPTION) // user wants to save
    {
        //get the file name from the filter:
        File save_file = fc.getSelectedFile();

        // check to see if the user entered an extension:
        if (!save_file.getName().endsWith(".csv") && !save_file.getName().endsWith(".CSV")) {
            save_file = new File(save_file.getAbsolutePath() + ".csv");
        }

        // create a string to save
        String s = toString();

        // IO inside a try/catch
        try {
            // Create a File Writer
            FileWriter fw = new FileWriter(save_file);
            fw.write(s);
            fw.close();
        }
        // If there was an error, launch an error dialog box:
        catch (IOException e) {
            DialogError emsg = new DialogError(this, " There was an error saving the file."
                    + System.getProperty("line.separator") + e.getMessage());
        }
    }
}

From source file:com.holycityaudio.SpinCAD.SpinCADFile.java

public void fileSaveSpj(SpinCADBank bank) {
    // Create a file chooser
    String savedPath = prefs.get("MRUSpjFolder", "");
    String[] spnFileNames = new String[8];

    final JFileChooser fc = new JFileChooser(savedPath);
    // In response to a button click:
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Spin Project Files", "spj");
    fc.setFileFilter(filter);//from  w w w.j  a  v  a 2 s.  c om
    // XXX debug
    fc.showSaveDialog(new JFrame());
    File fileToBeSaved = fc.getSelectedFile();

    if (!fc.getSelectedFile().getAbsolutePath().endsWith(".spj")) {
        fileToBeSaved = new File(fc.getSelectedFile() + ".spj");
    }
    int n = JOptionPane.YES_OPTION;
    if (fileToBeSaved.exists()) {
        JFrame frame1 = new JFrame();
        n = JOptionPane.showConfirmDialog(frame1, "Would you like to overwrite it?", "File already exists!",
                JOptionPane.YES_NO_OPTION);
    }
    if (n == JOptionPane.YES_OPTION) {
        // filePath points at the desired Spj file
        String filePath = fileToBeSaved.getPath();
        String folder = fileToBeSaved.getParent().toString();

        // export the individual SPN files
        for (int i = 0; i < 8; i++) {
            try {
                String asmFileNameRoot = FilenameUtils.removeExtension(bank.patch[i].patchFileName);
                String asmFileName = folder + "\\" + asmFileNameRoot + ".spn";
                if (bank.patch[i].patchFileName != "Untitled") {
                    fileSaveAsm(bank.patch[i], asmFileName);
                    spnFileNames[i] = asmFileName;
                }
            } catch (IOException e) {
                JOptionPane.showOptionDialog(null, "File save error!", "Error", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);

                e.printStackTrace();
            } finally {
            }
        }

        // now create the Spin Project file
        fileToBeSaved.delete();
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(fileToBeSaved, true));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            writer.write("NUMDOCS:8");
            writer.newLine();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        for (int i = 0; i < 8; i++) {
            try {
                if (bank.patch[i].patchFileName != "Untitled") {
                    writer.write(spnFileNames[i] + ",1");
                } else {
                    writer.write(",0");
                }
                writer.newLine();
            } catch (IOException e) {
                JOptionPane.showOptionDialog(null, "File save error!\n" + filePath, "Error",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

                e.printStackTrace();
            }
        }
        // write the build flags
        try {
            writer.write(",1,1,1");
            writer.newLine();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        saveMRUSpjFolder(filePath);
    }
}

From source file:calendarexportplugin.exporter.CalExporter.java

/**
 * Shows a file chooser for calendar Files.
 *
 * @return selected File//from  w ww.j av a2s. c  om
 * @param programs
 *          programs that are exported
 */
private File chooseFile(Program[] programs) {
    JFileChooser select = new JFileChooser();

    ExtensionFileFilter vCal = new ExtensionFileFilter(mExtension, mExtensionFilter);
    select.addChoosableFileFilter(vCal);
    String ext = "." + mExtension;

    if (mSavePath != null) {
        select.setSelectedFile(new File(mSavePath));
        select.setFileFilter(vCal);
    }

    // check if all programs have same title. if so, use as filename
    String fileName = programs[0].getTitle();
    for (int i = 1; i < programs.length; i++) {
        if (!programs[i].getTitle().equals(fileName)) {
            fileName = "";
        }
    }

    fileName = CalendarToolbox.cleanFilename(fileName);

    if (StringUtils.isNotEmpty(fileName)) {
        if (mSavePath == null) {
            mSavePath = "";
        }
        select.setSelectedFile(new File((new File(mSavePath).getParent()) + File.separator + fileName + ext));
    }

    if (select.showSaveDialog(
            CalendarExportPlugin.getInstance().getBestParentFrame()) == JFileChooser.APPROVE_OPTION) {

        String filename = select.getSelectedFile().getAbsolutePath();

        if (!filename.toLowerCase().endsWith(ext)) {
            if (filename.endsWith(".")) {
                filename = filename.substring(0, filename.length() - 1);
            }
            filename = filename + ext;
        }

        return new File(filename);
    }

    return null;
}