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:net.sf.mzmine.chartbasics.gui.swing.EChartPanel.java

/**
 * Opens a file chooser and gives the user an opportunity to save the chart in PNG format.
 *
 * @throws IOException if there is an I/O error.
 *///from w ww.jav a  2  s.  c om
@Override
public void doSaveAs() throws IOException {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(this.getDefaultDirectoryForSaveAs());
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            localizationResources.getString("PNG_Image_Files"), "png");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setFileFilter(filter);

    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String filename = fileChooser.getSelectedFile().getPath();
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        ChartUtils.saveChartAsPNG(new File(filename), getChart(), getWidth(), getHeight(),
                getChartRenderingInfo());
    }
}

From source file:gui.InboxPanel.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    try {//from w  w  w  . j a  v a 2 s  . c om
        String name = GmailAPI.getAttachmentFilename(curId);
        System.out.println(name);
        if (name == "None") {
            JOptionPane.showMessageDialog(this, "No file attached on this email");
        } else {
            JFileChooser chooser = new JFileChooser();
            File defFile = new File(name);
            chooser.setSelectedFile(defFile);
            int filesave = chooser.showSaveDialog(null);
            if (filesave == JFileChooser.APPROVE_OPTION) {
                //chooser.getSelectedFile().se
                File file = chooser.getCurrentDirectory();
                GmailAPI.getAttachments(GmailAPI.service, GmailAPI.USER, curId, file.getAbsolutePath(),
                        chooser.getSelectedFile().getName());
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.nekorp.workflow.desktop.view.CostoServicioView.java

private void generarReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generarReporteActionPerformed
    try {/*  w  w  w.  jav a2  s. c  o m*/
        if (servicioMetaData.isEditado()) {
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            this.aplication.guardaServicio();
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
        }
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Hojas de clculo", "xlsx");
        chooser.setFileFilter(filter);
        String homePath = System.getProperty("user.home");
        File f = new File(new File(homePath + "/Reporte-Servicio-" + this.viewServicioModel.getId() + ".xlsx")
                .getCanonicalPath());
        chooser.setSelectedFile(f);
        int returnVal = chooser.showSaveDialog(this.mainFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            ParametrosReporte param = new ParametrosReporte();
            param.setDestination(chooser.getSelectedFile());
            this.aplication.generaReporte(param);
        }
    } catch (IllegalArgumentException e) {
        //no lo guardo por que tenia horribles errores... tambien especializar la excepcion
    } catch (IOException ex) {
        CostoServicioView.LOGGER.error(ex);
    } finally {
        this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
    }
}

From source file:jpad.MainEditor.java

public void saveAs() {
    if (_isOSX || ostype == ostype.Linux || ostype == ostype.Other) {
        saveAs_OSX_Nix();//from   www.j  a va 2 s  .  c  o  m
        saveFile(curFile);
    } else /* Windows */
    {
        JFileChooser fc = new JFileChooser();
        if (curFile != null)
            fc.setSelectedFile(new File(curFile));
        else
            fc.setSelectedFile(new File("Untitled.txt"));
        fc.setDialogTitle("Save Text File");
        fc.setFileFilter(new FileNameExtensionFilter("Plain Text Files", "txt"));
        int returnval = fc.showSaveDialog(this);
        if (returnval == 0) {
            curFile = fc.getSelectedFile().getAbsolutePath();
            if (!curFile.endsWith(".txt"))
                curFile = curFile + ".txt";
            hasChanges = false;
            hasSavedToFile = true;
            saveFile(curFile);
        }
    }
}

From source file:com.evanbelcher.DrillBook.display.DBMenuBar.java

/**
 * Saves the current file under a new name/path
 *
 * @throws InterruptedException if there is an error when waiting for the saves to finish
 *///w w  w.  j av  a2s.c o  m
private void saveAs() throws InterruptedException {
    if (askToSave()) {

        final JFileChooser fc = new JFileChooser(Main.getFilePath());
        fc.setFileFilter(new DrillFileFilter());
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = fc.showSaveDialog(this);
        File file = fc.getSelectedFile();

        String name, path;

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                path = file.getCanonicalPath();
                path = path.substring(0, Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
                name = file.getName().toLowerCase().endsWith(".drill") ? file.getName()
                        : file.getName() + ".drill";
                Main.setFilePath(path);
                Main.setPagesFileName(name);
                Main.savePages().join();
                Main.saveState().join();
                Main.load(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:analysers.ExportValidated.java

@Override
public void analyze(Cell currentCell) {
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new FileNameExtensionFilter("Comma Separated Values", "csv"));
    fc.addChoosableFileFilter(new FileNameExtensionFilter("JSON file", "json"));
    int rv = fc.showSaveDialog(null);
    if (rv == JFileChooser.APPROVE_OPTION) {
        int b = Prefs.getInt(".TrackApp.FilterValidatedDialog.exportMean", 1);
        if (b != 0) {
            IJ.log("Exporting mean intensity values.");
        } else {//  w  ww.j av a  2  s  .com
            IJ.log("Exporting sum of intensity values.");
        }

        File ff = fc.getSelectedFile();
        String fn = fc.getSelectedFile().getAbsolutePath();

        if (fc.getFileFilter().getDescription().contains("JSON")) {
            if (!fn.toLowerCase().endsWith(".json")) {
                fn = fn + ".json";
                ff = new File(ff + ".json");
            }
            IJ.log("Writing a JSON file.");
        } else {
            if (!fn.toLowerCase().endsWith(".csv")) {
                fn = fn + ".csv";
                ff = new File(ff + ".csv");
            }
            IJ.log("Writing a CSV file.");
        }
        IJ.log("Writing to " + fn);

        if (fn.endsWith(".csv")) {
            writeCSV(ff);
        } else if (fn.endsWith(".json")) {
            writeJSON(ff);
        } else {
            IJ.log("Unknown output format for file name " + fn);
        }

    }
}

From source file:com.josue.tileset.editor.Editor.java

private void exportBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportBtnActionPerformed

    JFileChooser fileDialog = new JFileChooser();
    fileDialog.setFileFilter(new FileNameExtensionFilter("Json file", "json"));
    String outputFileName = inputFile.getAbsolutePath().split("\\.")[0] + ".json";
    fileDialog.setSelectedFile(new File(outputFileName));
    if (fileDialog.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

        File file = fileDialog.getSelectedFile();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        try {/*from w  ww.ja va  2 s .c  o  m*/
            mapper.writeValue(file, loadedTiles);
        } catch (IOException ex) {
            Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:be.vds.jtbdive.client.view.core.dive.profile.DiveProfileGraphicDetailPanel.java

private Component createExportButton() {
    exportButton = new JButton(new AbstractAction(null, UIAgent.getInstance().getIcon(UIAgent.ICON_EXPORT_24)) {

        private static final long serialVersionUID = 9021437098555924654L;

        @Override//from  w  w  w. ja  v  a 2s .co m
        public void actionPerformed(ActionEvent e) {
            JFileChooser ch = new JFileChooser();
            ch.setFileFilter(new ExtensionFilter("xls", "Excel File"));
            int i = ch.showSaveDialog(null);
            if (i == JFileChooser.APPROVE_OPTION) {
                DiveProfileExcelParser p = new DiveProfileExcelParser();
                try {
                    File f = ch.getSelectedFile();
                    if (FileUtilities.getExtension(f) == null
                            || !("xls".equals(FileUtilities.getExtension(f)))) {
                        f = new File(f.getAbsoluteFile() + ".xls");
                    }
                    p.export(currentDive.getDate(), diveProfile, f);
                } catch (IOException e1) {
                    ExceptionDialog.showDialog(e1, DiveProfileGraphicDetailPanel.this);
                    LOGGER.error(e1.getMessage());
                }
            }

        }
    });
    exportButton.setToolTipText("Export");
    return exportButton;
}

From source file:com.smanempat.controller.ControllerClassification.java

public void saveResultFile(JYearChooser thnAjaran1, JYearChooser thnAjaran2, JTable tableResult)
        throws IOException {
    JFileChooser dirChooser = new JFileChooser();
    dirChooser.setDialogTitle("Save as Excel File");
    String thnAjaran = Integer.toString(thnAjaran1.getYear()) + "-" + Integer.toString(thnAjaran2.getYear());
    String generateFileName = "Data Hasil Penjurusan Siswa Tahun Ajaran " + thnAjaran + ".xlsx";
    dirChooser.setSelectedFile(new File(generateFileName));
    int userSelection = dirChooser.showSaveDialog(null);
    int rowCountData = tableResult.getRowCount();
    if (userSelection == dirChooser.APPROVE_OPTION) {
        File fileToSave = dirChooser.getSelectedFile();
        convertToExcel(tableResult, fileToSave);
        JOptionPane.showMessageDialog(null, "Hasil klasifikasi jurusan berhasil disimpan",
                "Penyimpanan Berhasil", JOptionPane.INFORMATION_MESSAGE,
                new ImageIcon("src/com/smanempat/image/success.png"));
    }//from  ww w  .  j  a  v  a  2 s.  co  m
}

From source file:com.moteiv.trawler.Trawler.java

private void processEvent(AbstractButton source) {
    if (source == vLog) {
        if (source.isSelected()) {
            JFileChooser jfc = new JFileChooser();
            File logFile;//from   w  w w .  ja v a 2  s  .c om
            int retval;
            retval = jfc.showSaveDialog(null);
            if (retval == JFileChooser.APPROVE_OPTION) {
                mif.setLogFile(jfc.getSelectedFile());
            } else {
                vLog.setSelected(false);
            }
        } else {
            mif.setLogFile(null);
        }
    } else if (source == vLabels) {
        if (source.isSelected()) {
            pr.setVertexStringer(m_vs);
        } else {
            pr.setVertexStringer(new ConstantVertexStringer(null));
        }
    } else if (source == vBlink) {
        if (source.isSelected()) {
            pr.setVertexColorFunction(new myVertexColorFunction(Color.RED.darker().darker(), Color.RED, 500));
            ;
        } else {
            pr.setVertexPaintFunction(
                    new PickableVertexPaintFunction(pr, Color.BLACK, Color.RED, Color.ORANGE));
        }
    } else if (source == vSave) {
        if (source.isSelected()) {
        } else {
        }
    } else if (source == eLabels) {
        if (source.isSelected()) {
            pr.setEdgeStringer(m_es);
        } else {
            pr.setEdgeStringer(new ConstantEdgeStringer(null));
        }
    } else if (source == eFilter) {
        if (source.isSelected()) {
            pr.setEdgeIncludePredicate(TruePredicate.getInstance());
        } else {
            pr.setEdgeIncludePredicate(new myEdgeFilter());
        }
    } else if (source == graphReset) {
        GraphIO.resetPrefs(g, layout, mif, Trawler.NODEFILE);
    }
    savePrefs();
}