Example usage for javax.swing JFileChooser showDialog

List of usage examples for javax.swing JFileChooser showDialog

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public int showDialog(Component parent, String approveButtonText) throws HeadlessException 

Source Link

Document

Pops a custom file chooser dialog with a custom approve button.

Usage

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

@Override
protected void fileBrowseButtonActionPerformed(ActionEvent evt) {
    JFileChooser fileopen = new JFileChooser();
    fileopen.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int ret = fileopen.showDialog(this, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION) {
        fileLocationTextField.setText(fileopen.getSelectedFile().toString());
    }/*from w  w  w .j  av  a 2  s .co  m*/
}

From source file:net.aepik.alasca.gui.util.LoadFileFrame.java

/**
 * Perform action on event for this object.
 *//*from  w w  w. ja  v  a2 s .  c  o m*/
public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if (o == boutonOpenFile) {
        JFileChooser jfcProgramme = new JFileChooser(".");
        jfcProgramme.setMultiSelectionEnabled(false);
        jfcProgramme.setDialogTitle("Selectionner un fichier");
        jfcProgramme.setApproveButtonText("Selectionner");
        jfcProgramme.setApproveButtonToolTipText("Cliquer apres avoir selectionn un fichier");
        jfcProgramme.setAcceptAllFileFilterUsed(false);
        if (jfcProgramme.showDialog(this, null) == JFileChooser.APPROVE_OPTION) {
            try {
                filename.setText(jfcProgramme.getSelectedFile().getCanonicalPath());
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(null, "Erreur de nom de fichier.", "Erreur",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    if (o == boutonOk && filename.getText().length() != 0) {
        if (!this.loadFile(filename.getText(), (String) this.syntaxes.getSelectedItem())) {
            JOptionPane.showMessageDialog(this, this.getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
        } else {
            windowClosing(null);
        }
    }
    if (o == boutonAnnuler) {
        windowClosing(null);
    }
}

From source file:de.quadrillenschule.azocamsyncd.gui.ConfigurationWizardJFrame.java

private void localStorageDirjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localStorageDirjButton1ActionPerformed
    boolean success = false;
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.setSelectedFile(new File(gp.getProperty(CamSyncProperties.LOCALSTORAGE_PATH)));
    if (jfc.showDialog(jPanel1, "Choose as local directory") == JFileChooser.APPROVE_OPTION) {

        File f = new File(jfc.getSelectedFile().getAbsolutePath(), "azocamsynctest");
        success = f.mkdir();//ww  w. jav a  2 s.  c om
        success = f.delete();
        gp.setProperty(CamSyncProperties.LOCALSTORAGE_PATH, jfc.getSelectedFile().getAbsolutePath());
    }
    if (success) {
        JOptionPane.showMessageDialog(rootPane,
                "You've setup the path for incoming files.\nAll configuration steps are performed now.\n Let's start the service!",
                "Success!", JOptionPane.INFORMATION_MESSAGE);

        step2jCheckBox.setSelected(true);
        step2jCheckBox.setText("Success");
        this.setVisible(false);
    } else {
        JOptionPane.showConfirmDialog(rootPane, "This directory cannot read &write. Please choose another one.",
                "Local storage directory not working", JOptionPane.WARNING_MESSAGE);
    }

}

From source file:jhplot.HPlotChart.java

/**
 * Exports the image to some graphic format.
 *//*from   w ww  .  j  a v a2 s . c  o m*/
protected void exportImage() {

    JFrame jm = getFrame();
    JFileChooser fileChooser = jhplot.gui.CommonGUI.openImageFileChooser(jm);

    if (fileChooser.showDialog(jm, "Save As") == 0) {

        final File scriptFile = fileChooser.getSelectedFile();
        if (scriptFile == null)
            return;
        else if (scriptFile.exists()) {
            int res = JOptionPane.showConfirmDialog(jm, "The file exists. Do you want to overwrite the file?",
                    "", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.NO_OPTION)
                return;
        }
        String mess = "write image  file ..";
        JHPlot.showStatusBarText(mess);
        Thread t = new Thread(mess) {
            public void run() {
                export(scriptFile.getAbsolutePath());
            };
        };
        t.start();
    }
}

From source file:jhplot.HPlotChart.java

/**
* Exports the image to some graphic format.
*///from  w w  w  . j a v a  2 s .c o  m
private void exportDialog() {

    final JFileChooser fc = jhplot.gui.CommonGUI.openImageFileChooser(frame);

    if (fc.showDialog(frame, "Export to ") == 0) {
        // final File scriptFile = fileChooser.getSelectedFile();
        final File scriptFile = jhplot.io.images.ExportVGraphics.getSelectedFileWithExtension(fc);
        // System.out.println(scriptFile.getAbsolutePath());

        if (scriptFile == null)
            return;
        else if (scriptFile.exists()) {
            int res = JOptionPane.showConfirmDialog(frame,
                    "The file exists. Do you want to overwrite the file?", "", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.NO_OPTION)
                return;
        }
        String mess = "write image  file ..";
        Thread t = new Thread(mess) {
            public void run() {
                export(scriptFile.getAbsolutePath());
            };
        };
        t.start();
    }

}

From source file:com.genericworkflownodes.knime.nodes.io.OutputFileNodeDialog.java

/**
 * New pane for configuring MimeFileExporter node dialog.
 *///from ww  w.j  a  v  a 2  s .  c o  m
public OutputFileNodeDialog(final String settingsName) {
    this.settingsName = settingsName;
    dialogPanel = new JPanel();
    componentContainer = new JPanel();
    textField = new JTextField();
    textField.setPreferredSize(new Dimension(300, textField.getPreferredSize().height));
    searchButton = new JButton("Browse");
    searchButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser jfc = new JFileChooser();
            if (!"".equals(textField.getText().trim())
                    && new File(textField.getText().trim()).getParent() != null) {
                jfc.setCurrentDirectory(new File(textField.getText().trim()).getParentFile());
            }

            jfc.setAcceptAllFileFilterUsed(false);
            jfc.setFileFilter(extensionFilter);

            // int returnVal = jfc.showSaveDialog(dialogPanel);
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = jfc.showDialog(dialogPanel, "Select output file");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                // validate extension
                if (!extensionFilter.accept(jfc.getSelectedFile())) {
                    String message = "The selected output file has an invalid file extension.\n";
                    if (extensionFilter.getExtensions().length == 1) {
                        message += "Please choose a file with extension: " + extensionFilter.getExtensions()[0];
                    } else {
                        message += "Please choose a file with on of the following extensions: ";
                        message += StringUtils.join(extensionFilter.getExtensions(), ", ");
                    }
                    JOptionPane.showMessageDialog(getPanel(), message, "Selected Output File is invalid.",
                            JOptionPane.WARNING_MESSAGE);
                }
                textField.setText(jfc.getSelectedFile().getAbsolutePath());
            }
        }
    });
    setLayout();
    addComponents();

    addTab("Choose File", dialogPanel);
}

From source file:storybook.ui.chart.AbstractChartPanel.java

private AbstractAction getExportAction() {
    if (this.exportAction == null) {
        this.exportAction = new AbstractAction() {
            @Override/*from   ww w. ja  va 2s  .  c o m*/
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
                try {
                    Internal localInternal = BookUtil.get(AbstractChartPanel.this.mainFrame,
                            SbConstants.BookKey.EXPORT_DIRECTORY,
                            EnvUtil.getDefaultExportDir(AbstractChartPanel.this.mainFrame));
                    File localFile1 = new File(localInternal.getStringValue());
                    JFileChooser localJFileChooser = new JFileChooser(localFile1);
                    localJFileChooser.setFileFilter(new PngFileFilter());
                    localJFileChooser.setApproveButtonText(I18N.getMsg("msg.common.export"));
                    String str = AbstractChartPanel.this.mainFrame.getDbFile().getName() + " - "
                            + AbstractChartPanel.this.chartTitle;
                    str = IOUtil.cleanupFilename(str);
                    localJFileChooser.setSelectedFile(new File(str));
                    int i = localJFileChooser.showDialog(AbstractChartPanel.this.getThis(),
                            I18N.getMsg("msg.common.export"));
                    if (i == 1) {
                        return;
                    }
                    File localFile2 = localJFileChooser.getSelectedFile();
                    if (!localFile2.getName().endsWith(".png")) {
                        localFile2 = new File(localFile2.getPath() + ".png");
                    }
                    ScreenImage.createImage(AbstractChartPanel.this.panel, localFile2.toString());
                    JOptionPane.showMessageDialog(AbstractChartPanel.this.getThis(),
                            I18N.getMsg("msg.common.export.success"), I18N.getMsg("msg.common.export"), 1);
                } catch (HeadlessException | IOException localException) {
                }
            }
        };
    }
    return this.exportAction;
}

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/*  w w w  .j  av  a2  s  . c  om*/
        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:au.org.ala.delta.editor.ui.image.ImageSettingsDialog.java

@Action
public void addToImagePath() {
    JFileChooser chooser = new JFileChooser(_imageSettings.getDataSetPath());
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = chooser.showDialog(this, _resources.getString("okImageSettingsChanges.Action.text"));
    if (result == JFileChooser.APPROVE_OPTION) {
        _imageSettings.addToResourcePath(chooser.getSelectedFile());
        imagePathTextField.setText(_imageSettings.getResourcePath());
    }/*from   w ww .j a v  a2s .co  m*/
}

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

/**
 * Creates a new json file for a new show
 *
 * @throws InterruptedException if there is an error when waiting for the saves to finish
 *//*from   w  w w . ja v a2 s  .c o m*/
private void newShow() throws InterruptedException {
    if (askToSave()) {
        final JFileChooser fc = new JFileChooser(new File(Main.getFilePath()));
        fc.setFileFilter(new DrillFileFilter());
        int returnVal = fc.showDialog(this, "New File");

        String name, path;

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile();
                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.saveState().join();
                Main.load(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}