Example usage for javax.swing JFileChooser showOpenDialog

List of usage examples for javax.swing JFileChooser showOpenDialog

Introduction

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

Prototype

public int showOpenDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up an "Open File" file chooser dialog.

Usage

From source file:com.nwn.NwnUpdaterHomeView.java

/**
 * Allow user to specify their NWN directory
 * @param evt /*from w ww .  j  a  v a  2s.c  o m*/
 */
private void btnSelectNwnDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectNwnDirActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("."));
    fc.setDialogTitle("NWN Updater");
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setAcceptAllFileFilterUsed(false);
    int returnVal = fc.showOpenDialog(txtNwnDir);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        txtNwnDir.setText(file.getAbsolutePath());
    }
}

From source file:org.martus.client.swingui.FxInSwingMainWindow.java

public File showFileOpenDialog(String title, File directory, Vector<FormatFilter> filters) {
    JFileChooser fileChooser = createFileChooser(title, directory, filters);

    int userResult = fileChooser.showOpenDialog(getCurrentActiveFrame().getSwingFrame());
    if (userResult != JFileChooser.APPROVE_OPTION)
        return null;

    return fileChooser.getSelectedFile();
}

From source file:net.sf.profiler4j.console.Console.java

public void openProject() {

    if (client.isConnected()) {
        int ret = JOptionPane.showConfirmDialog(mainFrame, "Proceed and disconnect?", "Open Profiling Project",
                JOptionPane.YES_NO_OPTION);
        if (ret == JOptionPane.NO_OPTION) {
            return;
        }/*from  www .j  a  v a 2  s .com*/
    }

    if (checkUnsavedChanges()) {
        return;
    }

    if (client.isConnected()) {
        disconnect();
        if (client.isConnected()) {
            return;
        }
    }
    JFileChooser fc = new JFileChooser(lastDir);
    fc.addChoosableFileFilter(projectFilter);
    if (fc.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
        File selFile = fc.getSelectedFile();
        SAXBuilder builder = new SAXBuilder();
        Document doc = null;
        try {
            doc = builder.build(selFile);
        } catch (JDOMException e) {
            error("XML Error", e);
        } catch (IOException e) {
            error("I/O Error", e);
        }
        if (doc != null) {
            Project p = new Project();
            Element el = doc.getRootElement();
            p.setHostname(el.getChildText("Host"));
            p.setPort(Integer.parseInt(el.getChildText("Port")));
            Element rulesEl = el.getChild("Rules");

            p.setAccess(Rule.AccessOption.valueOf(rulesEl.getAttributeValue("access")));
            p.setBeanprops(Boolean.parseBoolean(rulesEl.getAttributeValue("beanProps")));

            p.getRules().clear();
            for (Iterator i = rulesEl.getChildren("Rule").iterator(); i.hasNext();) {
                Element r = (Element) i.next();
                Rule rule = new Rule(r.getText(), Rule.Action.valueOf(r.getAttributeValue("action")));
                p.getRules().add(rule);
            }

            // Backwards compatible way to read the export pattern
            // If it is not there, we leave the defaults as they are,
            // otherwise we set the saved setting.
            Element export = el.getChild(PROJECT_XML_ELEMENT__EXPORT_PATTERN);
            if (null != export) {
                String enabled = export.getAttributeValue(PROJECT_XML_ATTRIBUTE__ENABLED);
                p.setExportAutomaticallyEnabled(Boolean.valueOf(enabled));
                p.setExportPattern(export.getAttributeValue(PROJECT_XML_ATTRIBUTE__PATTERN));
            }

            p.setFile(selFile);
            p.clearChanged();
            this.project = p;
            lastDir = selFile.getParentFile();
        }
    }

}

From source file:baocaoxla.compare.java

private void btOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOutputActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.showOpenDialog(this);
    File dir = fc.getSelectedFile();
    txtOutput.setText(dir.toString());/*w w  w.  j av a 2 s . com*/
}

From source file:au.org.ala.delta.editor.ui.ImageDetailsPanel.java

private File getMediaFile(FileFilter filter) {
    String imagePath = _dataSet.getImagePath();
    JFileChooser chooser = new JFileChooser(imagePath);
    chooser.setFileFilter(filter);//from  w ww . j  a va  2  s. c  o  m

    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        File imageFile = chooser.getSelectedFile();

        ImageSettings settings = _dataSet.getImageSettings();
        if (settings.isOnResourcePath(imageFile)) {

            String name = imageFile.getName();
            boolean exists = false;

            if (_selectedImage != null) {
                List<ImageOverlay> existingSounds = _selectedImage.getSounds();
                for (ImageOverlay sound : existingSounds) {
                    if (name.equals(sound.overlayText)) {
                        exists = true;
                        break;
                    }
                }
            }
            if (exists) {
                int result = _messageHelper.confirmDuplicateFileName();
                if (result == JOptionPane.YES_OPTION) {
                    imageFile = new File(imageFile.getAbsolutePath());
                } else if (result == JOptionPane.NO_OPTION) {
                    return getMediaFile(filter);
                } else {
                    imageFile = null;
                }
            } else {
                // Turn the file into a relative one.
                imageFile = new File(name);
            }
        } else {
            // Ask about it or copy it to the image path.
            int result = _messageHelper.confirmNotOnImagePath();
            if (result == JOptionPane.YES_OPTION) {
                imageFile = new File(imageFile.getAbsolutePath());
            } else if (result == JOptionPane.NO_OPTION) {
                return getMediaFile(filter);
            } else {
                imageFile = null;
            }
        }

        return imageFile;
    }

    return null;
}

From source file:view.FramePrincipal.java

public String retornaDiretorioArquivo(String[] TipoArquivo) {
    String dir = "";
    JFileChooser _fileChooser = new JFileChooser();

    int retval = _fileChooser.showOpenDialog(this);
    final String[] okFileExtensions = TipoArquivo;
    File file;/*w  w  w  .j  a  va 2  s .c  o m*/
    if (retval == JFileChooser.APPROVE_OPTION) {
        file = _fileChooser.getSelectedFile();
        Boolean flag = false;

        for (String extension : okFileExtensions) {
            if (file.getName().toLowerCase().endsWith(extension)) {
                flag = true;
            }
        }
        if (!flag) {
            JOptionPane.showMessageDialog(this,
                    "Selecione um Arquivo do tipo " + java.util.Arrays.toString(TipoArquivo), "Erro",
                    JOptionPane.ERROR_MESSAGE);
            retornaDiretorioArquivo(TipoArquivo);
        }

        if (_fileChooser.getSelectedFile().exists()) {
            dir = _fileChooser.getSelectedFile().toString();
        }
        LimpaPanels();
    }
    return dir;
}

From source file:baocaoxla.compare.java

private void btInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInputActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.showOpenDialog(this);
    File dir = fc.getSelectedFile();
    txtInput.setText(dir.toString());//from w w  w.  j ava  2  s.c  o  m
    /* File[] file=dir.listFiles(new FilenameFilter() {
            
     @Override
     public boolean accept(File dir, String name) {
     return name.toLowerCase().endsWith(".jpg"); //To change body of generated methods, choose Tools | Templates.
     }
     });*/
    //File[] file = dir.listFiles();
    //for (int i = 0; i < file.length; i++) {
    //  System.out.println(file[i].getName());
    //}

}

From source file:GUI.MyCustomFilter.java

private void NewFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewFileActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("choosertitle");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getAbsolutePath();
        String ext = FilenameUtils.getExtension(path);
        outputPath = path;/*from w w  w .  j a v  a 2 s.  c om*/
        projectSelected = 1;
        //System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
        //System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
    } else {
        System.out.println("No Selection ");
    }
}

From source file:aurelienribon.gdxsetupui.ui.panels.LibrarySelectionPanel.java

private void browse(String libraryName) {
    File file = libsSelectedFiles.get(libraryName);
    String path = file != null ? file.getPath() : ".";
    JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);

    JFileChooser chooser = new JFileChooser(new File(path));
    chooser.setFileFilter(new FileNameExtensionFilter("Zip files (*.zip)", "zip"));
    chooser.setDialogTitle("Please select the zip archive for \"" + libraryName + "\"");

    if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
        select(libraryName, chooser.getSelectedFile());
    }/*from   w w w.j  a  v  a 2  s.  co m*/
}