Example usage for javax.swing JFileChooser ERROR_OPTION

List of usage examples for javax.swing JFileChooser ERROR_OPTION

Introduction

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

Prototype

int ERROR_OPTION

To view the source code for javax.swing JFileChooser ERROR_OPTION.

Click Source Link

Document

Return value if an error occurred.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(null);
    switch (result) {
    case JFileChooser.APPROVE_OPTION:
        System.out.println("Approve (Open or Save) was clicked");
        break;//  ww  w .  j  a  v a 2s.  c o m
    case JFileChooser.CANCEL_OPTION:
        System.out.println("Cancel or the close-dialog icon was clicked");
        break;
    case JFileChooser.ERROR_OPTION:
        System.out.println("Error");
        break;
    }
}

From source file:Main.java

public static void main(String[] args) {
    String path = System.getProperty("user.dir", ".");

    File dir = new File(path);

    JFileChooser jfc = new JFileChooser(dir);
    int result = jfc.showOpenDialog(null);

    switch (result) {
    case JFileChooser.CANCEL_OPTION:
        System.out.println("User cancelled OPEN dialog.");
        break;/*from w w  w. ja  va2 s  .  co m*/
    case JFileChooser.APPROVE_OPTION:
        System.out.println("User chose file: " + jfc.getSelectedFile());
        break;
    case JFileChooser.ERROR_OPTION:
        System.out.println("User encountered an error");
        break;
    default:
        System.out.println("Confused");
        break;
    }

    System.exit(0);
}

From source file:SwingUtil.java

/**
 * Open a JFileChooser dialog for selecting a directory and return the
 * selected directory.//from  w  w  w  .ja v a  2  s .c  o m
 *
 *
 * @param owner
 * The frame or dialog that controls the invokation of this dialog.
 * @param defaultDir
 * The directory to show when the dialog opens.
 * @param title
 * Tile for the dialog.
 *
 * @return
 * The selected directory as a File. Null if user cancels dialog without
 * a selection.
 *
 */
public static File getDirectoryChoice(Component owner, File defaultDir, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    JFileChooser chooser = null;
    File choice = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);
    chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    if ((defaultDir != null) && defaultDir.exists() && defaultDir.isDirectory()) {
        chooser.setCurrentDirectory(defaultDir);
        chooser.setSelectedFile(defaultDir);
    }
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            if (chooser.getSelectedFile().exists()) {
                choice = chooser.getSelectedFile();
            } else {
                File parentFile = new File(chooser.getSelectedFile().getParent());

                choice = parentFile;
            }
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:SwingUtil.java

/**
 * Get a file selection using the FileChooser dialog.
 *
 * @param owner/*from   www . ja v  a 2  s .c  om*/
 * The parent of this modal dialog.
 * @param defaultSelection
 * The default file selection as a file.
 * @param filter
 * An extension filter
 * @param title
 * The caption for the dialog.
 *
 * @return
 * A selected file or null if no selection is made.
 */
public static File getFileChoice(Component owner, File defaultSelection, FileFilter filter, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    File choice = null;
    JFileChooser chooser = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);

    chooser = new JFileChooser();
    if (defaultSelection.isDirectory()) {
        chooser.setCurrentDirectory(defaultSelection);
    } else {
        chooser.setSelectedFile(defaultSelection);
    }
    chooser.setFileFilter(filter);
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            choice = chooser.getSelectedFile();
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.SaveChartDialog.java

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == btnCancel) {
        // Cancel button pressed -> close the window
        this.setVisible(false);
        this.dispose();
    } else if (source == btnSave) {
        // Save button pressed -> choose file name and save the chart
        int saveIt = saveFileDialog.showSaveDialog(this);
        if (saveIt == JFileChooser.APPROVE_OPTION) {

            // Choose file name
            File file = saveFileDialog.getSelectedFile();
            int width = getChosenWidth();
            int height = getChosenHeight();
            ExtensionFileFilter filter = null;
            try {
                filter = (ExtensionFileFilter) saveFileDialog.getFileFilter();
                if (!filter.hasExtension(file)) {
                    file = filter.appendExtension(file);
                }/*from  www .j  ava 2 s.  c  om*/
            } catch (ClassCastException ex) {
                // Try to infer the type of file by its extension
                FileFilter[] filters = saveFileDialog.getChoosableFileFilters();
                for (int i = 0; i < filters.length; ++i) {
                    if (filters[i] instanceof ExtensionFileFilter) {
                        filter = (ExtensionFileFilter) filters[i];
                        if (filter.hasExtension(file)) {
                            break;
                        }
                        filter = null;
                    }
                }

                if (filter == null) {
                    // Could not infer the type
                    Utils.showErrorBox(this, Messages.DT_IOERROR, Messages.SM_AMBIGUOUSFTYPE);
                    return;
                }
            }

            // Save the chart to the specified file name
            try {
                if (Utils.canSave(file, this)) {
                    String ext = filter.getExtension();
                    if (ext.equals("jpeg")) {
                        JFreeChartConn.saveAsJpeg(file, chart, width, height);
                    } else if (ext.equals("png")) {
                        JFreeChartConn.saveAsPng(file, chart, width, height);
                    } else { // ext.equals("svg")
                        JFreeChartConn.saveAsSvg(file, chart, width, height);
                    }
                }
            } catch (IOException ex) {
                Utils.showErrorBox(this, Messages.DT_IOERROR, Messages.SM_OERROR);
                return;
            }
            this.setVisible(false);
            this.dispose();
        } else if (saveIt == JFileChooser.ERROR_OPTION) {
            Utils.showErrorBox(this, Messages.DT_GUIERROR, Messages.SM_GUIERROR);
        }
    }
}

From source file:com.rpgsheet.xcom.PimpMyXcom.java

private static void openXcomPathDialog(Properties appProperties) {
    // display the box to the user
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("PimpMyXcom - Where is X-COM?");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int retCode = fileChooser.showOpenDialog(null);
    // if the user cancelled the dialog, there is nothing we can do
    if (retCode == JFileChooser.CANCEL_OPTION) {
        log.error("User cancel while attempting to locate X-COM.");
        return;/*from   ww w  .  j ava 2  s.  c om*/
    }
    // if there was an error, there is nothing we can do
    if (retCode == JFileChooser.ERROR_OPTION) {
        log.error("Error while attempting to locate X-COM: {}", JFileChooser.ERROR_OPTION);
        return;
    }
    // if the user chose a directory, then we have some work to do
    String xcomPath = fileChooser.getSelectedFile().getAbsolutePath();
    File xcomDir = new File(xcomPath);
    if (containsXcom(xcomDir)) {
        // store the path to xcom in the application properties
        appProperties.setProperty("xcom.path", xcomPath);
        // write the updated application properties to disk
        try {
            saveApplicationProperties(appProperties);
        } catch (IOException e) {
            log.error("Unable to store X-COM path in application properties.", e);
            return;
        }
    }
    // if we were unable to find X-COM
    else {
        log.error("User provided location did not contain X-COM.");
        return;
    }
}

From source file:org.cytoscape.dyn.internal.graphMetrics.SaveChartDialog.java

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == cancelButton) {
        this.setVisible(false);
        this.dispose();
    } else if (e.getSource() == saveChartButton) {
        JFileChooser saveFileDialog = new JFileChooser();
        saveFileDialog/*from  ww  w .ja  v a  2  s . c  o m*/
                .addChoosableFileFilter(new ExtensionFileFilter(".jpeg", ".jpg", "Jpeg images (.jpeg, .jpg)"));
        saveFileDialog.addChoosableFileFilter(
                new ExtensionFileFilter(".png", "Portable Network Graphic images (.png)"));
        saveFileDialog
                .addChoosableFileFilter(new ExtensionFileFilter(".svg", "Scalable Vector Graphics (.svg)"));
        int save = saveFileDialog.showSaveDialog(this);
        if (save == JFileChooser.APPROVE_OPTION) {
            File file = saveFileDialog.getSelectedFile();
            int width = ((SpinnerNumberModel) widthSpinner.getModel()).getNumber().intValue();
            int height = ((SpinnerNumberModel) heightSpinner.getModel()).getNumber().intValue();

            ExtensionFileFilter filter = null;
            try {
                filter = (ExtensionFileFilter) saveFileDialog.getFileFilter();
                if (!filter.hasExtension(file)) {
                    file = filter.appendExtension(file);
                }
            } catch (ClassCastException ex) {
                // Try to infer the type of file by its extension
                FileFilter[] filters = saveFileDialog.getChoosableFileFilters();
                for (int i = 0; i < filters.length; ++i) {
                    if (filters[i] instanceof ExtensionFileFilter) {
                        filter = (ExtensionFileFilter) filters[i];
                        if (filter.hasExtension(file)) {
                            break;
                        }
                        filter = null;
                    }
                }

                if (filter == null) {
                    // Could not infer the type
                    JOptionPane.showMessageDialog(null,
                            "File type not specified!\nWhen giving file name, please also select one of the supported file types.",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            // Save the chart to the specified file name
            try {
                String ext = filter.getExtension();
                if (ext.equals("jpeg")) {
                    ChartUtilities.saveChartAsJPEG(file, chart, width, height);
                } else if (ext.equals("png")) {
                    ChartUtilities.saveChartAsPNG(file, chart, width, height);
                } else {
                    VectorGraphics graphics = new SVGGraphics2D(file, new Dimension(width, height));
                    graphics.startExport();
                    chart.draw(graphics, new Rectangle2D.Double(0, 0, width, height));
                    graphics.endExport();
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "An error occurred while creating or writing to the file.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.setVisible(false);
            this.dispose();
        } else if (save == JFileChooser.ERROR_OPTION) {
            JOptionPane.showMessageDialog(null, "An error occurred while initializing the window.", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

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

/**
 * Gets the show to open and opens the respective json file.
 *//*  w ww .  j  a v a  2s .co m*/
private void openShow() {
    if (askToSave()) {

        File file;
        final JFileChooser fc = new JFileChooser(Main.getFilePath());
        fc.setFileFilter(new DrillFileFilter());
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal;
        do {
            returnVal = fc.showOpenDialog(this);
            file = fc.getSelectedFile();
        } while (returnVal != JFileChooser.CANCEL_OPTION && returnVal != JFileChooser.ERROR_OPTION
                && !file.exists());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                String name, path;
                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.load(false);
                desktop.createNewInternalFrames();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:no.java.swing.SingleSelectionFileDialog.java

private Result showJFileChooser(Component target, boolean open) {
    JFileChooser chooser = new JFileChooser(previousDirectory);
    chooser.setMultiSelectionEnabled(false);
    chooser.setFileFilter(filter);/*w  ww.  j  a v a2s .c  o  m*/
    chooser.setAcceptAllFileFilterUsed(filter == null);
    chooser.setDialogType(open ? JFileChooser.OPEN_DIALOG : JFileChooser.SAVE_DIALOG);
    int selection = chooser.showDialog(target, null);

    switch (selection) {
    case JFileChooser.APPROVE_OPTION:
        this.selected = chooser.getSelectedFile();
        if (rememberPreviousLocation) {
            previousDirectory = chooser.getCurrentDirectory();
        }
        return Result.APPROVE;

    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    default:
        this.selected = null;
        return Result.ERROR;
    }
}

From source file:org.colombbus.tangara.net.FileReceptionDialog.java

private void setTargetFile() {
    File targetDir = Configuration.instance().getUserHome();
    JFileChooser chooserDlg = new JFileChooser(targetDir);

    String filterMsg = Messages.getString("FileReceptionDialog.filter.allFiles");
    SimpleFileFilter filter = new SimpleFileFilter(filterMsg);
    chooserDlg.setFileFilter(filter);/*w ww . j  av  a2  s .  c  o  m*/

    File originalSelFile = new File(targetDir, sourceFilename);
    File curSelFile = originalSelFile;
    boolean showDialog = true;
    while (showDialog) {
        chooserDlg.setSelectedFile(curSelFile);
        int userAction = chooserDlg.showSaveDialog(owner);
        curSelFile = chooserDlg.getSelectedFile();
        switch (userAction) {
        case JFileChooser.CANCEL_OPTION:
            showDialog = false;
            break;
        case JFileChooser.APPROVE_OPTION:
            if (curSelFile.exists()) {
                String title = Messages.getString("FileReceptionDialog.overwrite.title");
                String msgPattern = Messages.getString("FileReceptionDialog.overwrite.message");
                String overwriteMsg = MessageFormat.format(msgPattern, curSelFile.getName());
                Object[] options = { Messages.getString("FileReceptionDialog.yes"),
                        Messages.getString("FileReceptionDialog.no") };
                int userChoice = JOptionPane.showOptionDialog(owner, overwriteMsg, title,
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a
                        // custom Icon
                        options, // the titles of buttons
                        options[0]);
                if (userChoice == JOptionPane.OK_OPTION) {
                    if (saveFileAs(curSelFile)) {
                        showDialog = false;
                    }
                }
            } else if (saveFileAs(curSelFile)) {
                showDialog = false;
            }
            break;
        case JFileChooser.ERROR_OPTION:
            LOG.error("Error in file chooser dialog");
            // TODO what to do in case of error ? Retry ?
            showDialog = false;
            break;
        }
    }
}