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.redstonelamp.gui.RedstoneLampGUI.java

public static void main(String[] args) {
    JFrame frame = new JFrame("RedstoneLamp");
    frame.setLayout(new GridLayout(2, 1));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JLabel label = new JLabel("RedstoneLamp");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    frame.add(label);//  w w  w. java  2s  .  c  o m
    JPanel lowPanel = new JPanel();
    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    lowPanel.add(left);
    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    lowPanel.add(right);
    JButton openButton = new JButton("Open server at...");
    openButton.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser(new File("."));
        chooser.setDialogTitle("Select RedstoneLamp server home");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int action = chooser.showOpenDialog(frame);
        if (action == JFileChooser.APPROVE_OPTION) {
            File selected = chooser.getSelectedFile();
            File jar = new File("RedstoneLamp.jar");
            if (!jar.isFile()) {
                int result = JOptionPane.showConfirmDialog(frame, "Could not find RedstoneLamp installation. "
                        + "Would you like to install RedstoneLamp there?");
                if (result == JOptionPane.YES_OPTION) {
                    installCallback(frame, selected);
                }
                return;
            }
            frame.dispose();
            addHistory(selected);
            currentRoot = new ServerActivity(selected);
        }
    });
    right.add(openButton);
    JButton installButton = new JButton("Install server at...");
    installButton.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser(".");
        chooser.setDialogTitle("Select directory to install server in");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int action = chooser.showSaveDialog(frame);
        if (action == JFileChooser.APPROVE_OPTION) {
            File selected = chooser.getSelectedFile();
            File jar = new File("RedstoneLamp.jar");
            if (jar.isFile()) {
                int result = JOptionPane.showConfirmDialog(frame, "A RedstoneLamp jar installation is present. "
                        + "Are you sure you want to reinstall RedstoneLamp there?");
                if (result == JOptionPane.NO_OPTION) {
                    frame.dispose();
                    addHistory(selected);
                    currentRoot = new ServerActivity(selected);
                    return;
                }
            }
            installCallback(frame, selected);
        }
    });
    frame.add(lowPanel);
    frame.pack();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dimension.width / 2 - frame.getSize().width / 2,
            dimension.height / 2 - frame.getSize().height / 2);
    frame.setVisible(true);
}

From source file:org.jfreechart.SVGExporter.java

public static Action createExportAction(final JFreeChart chart, final JPanel bounds) {
    return new AbstractAction("Save as SVG...") {

        public void actionPerformed(ActionEvent e) {
            try {
                JFileChooser fc = new JFileChooser();
                fc.showSaveDialog(null);
                if (fc.getSelectedFile() != null) {
                    exportChartAsSVG(chart, bounds.getBounds(), fc.getSelectedFile());
                }/*from   w w w .  j av a2 s  .  c om*/
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    };
}

From source file:Main.java

/**
 * /*from   w w  w  . j a  va2  s .c o m*/
 * @param owner
 * @return El directorio seleccionado
 */
public static File saveFileChooser(Window owner) {
    int userResponse;
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("XML", "xml");
    fileChooser.setFileFilter(filter);
    userResponse = fileChooser.showSaveDialog(owner);
    if (JFileChooser.APPROVE_OPTION == userResponse) {
        return fileChooser.getSelectedFile();
    }
    return null;
}

From source file:jgraph.JShow.java

/**
 * a driver for this demo/*  w  ww.ja  v  a2  s . com*/
 */
@SuppressWarnings("serial")
public static void showtest(DirectedOrderedSparseMultigraph<Object, Object> graph) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JShow demo = new JShow(graph);

    JMenu menu = new JMenu("Snapshot");
    menu.add(new AbstractAction("To JPEG") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java

private static void addExportPopupMenu(final Instances ds, final ChartPanel cp) {
    cp.addChartMouseListener(new ChartMouseListener() {
        public void chartMouseClicked(ChartMouseEvent e) {
            final JPopupMenu jPopupMenu = new JPopupMenu("feur");
            final JMenuItem mi1 = new JMenuItem("Export as CSV");
            mi1.addActionListener(new ActionListener() {
                @Override//w  w w.j  a va2  s  . c  o  m
                public void actionPerformed(final ActionEvent e) {
                    final JFileChooser fc = new JFileChooser();
                    fc.setAcceptAllFileFilterUsed(false);
                    final int returnVal = fc.showSaveDialog(cp);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        try {
                            final File file = fc.getSelectedFile();
                            WekaDataAccessUtil.saveInstancesIntoCSVFile(ds, file);
                        } catch (final Exception ee) {
                            ee.printStackTrace();
                        }
                    }
                }
            });
            jPopupMenu.add(mi1);
            jPopupMenu.show(cp, e.getTrigger().getX(), e.getTrigger().getY());
        }

        public void chartMouseMoved(ChartMouseEvent e) {
        }
    });
}

From source file:my.grafos.Maquina.java

static void salvarGrafo() throws FileNotFoundException, IOException {
    /*//www  .j av a 2 s .  c  o  m
    FileOutputStream fos = new FileOutputStream("d:\\t.xml");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(Fabrica.listaArestas);
    oos.close();
     */
    final JFileChooser fc = new JFileChooser();
    //Handle save button action.
    int returnVal = fc.showSaveDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        System.out.println("1 " + fc.getSelectedFile());
        System.out.println("2 " + file);
        //            if (Fabrica.xmlA.length() == 0) {
        if (Fabrica.xmlA.length() == 0) {
            //                System.out.println("No h dados para salvar.");
            return;
        }
        try (OutputStream output = new FileOutputStream(file.getPath())) {
            int count = 0;

            while (count < Fabrica.xmlA.length()) {
                output.write(Fabrica.xmlA.charAt(count));
                count++;
            }
            count = 0;
            while (count < Fabrica.xmlV.length()) {
                output.write(Fabrica.xmlV.charAt(count));
                count++;
            }
        } catch (IOException e) {
        }
    }

}

From source file:grafix.principal.Comandos.java

static public void cmdSalvarJPEG() {
    ControleRegistro.alertaRegistro();//w  ww .j a  v a2 s  .  co  m
    JFileChooser chooser = new JFileChooser();
    chooser.setSelectedFile(new File(".jpg"));
    int returnVal = chooser.showSaveDialog(Controle.getTela());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            ChartUtilities.saveChartAsJPEG(file, Controle.getJanelaAtiva().getPanelGraficos().getChart(),
                    Controle.getJanelaAtiva().getWidth(), Controle.getJanelaAtiva().getHeight());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

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  ww  w . j a  v  a 2 s .co  m*/
            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:Main.java

public static File saveImageFile(Component parent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileFilter() {
        @Override//from w  w  w  . java 2 s .  co  m
        public String getDescription() {
            return "bmp";
        }

        @Override
        public boolean accept(File f) {
            String name = f.getName();
            boolean accepted = f.isDirectory() || name.endsWith(".bmp");
            return accepted;
        }
    });

    fileChooser.showSaveDialog(parent);
    return fileChooser.getSelectedFile();
}

From source file:grafix.principal.Comandos.java

static public void cmdSalvarConfiguracao() {
    ControleRegistro.alertaRegistro();//from   w  ww .  j  av a2s  .  c  om
    JFileChooser chooser = new JFileChooser(new File(ConfiguracoesGrafix.PASTA_TEMPLATES));
    chooser.setSelectedFile(new File(ConfiguracoesGrafix.EXTENSAO_TEMPLATES));
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    int returnVal = chooser.showSaveDialog(Controle.getTela());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            Controle.getConfiguracoesUsuario().setNome(file.getName());
            Controle.salvarConfiguracoesUsuario(true);
            LeitorArquivoConfiguracao.getInstance().criarCopia(file);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Controle.getTela().getComboConfiguracoes().popularCombo();
}