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.gnadenheimer.mg.frames.admin.FrameConfigAdmin.java

private void cmdDatadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDatadirActionPerformed
    try {/*from  w ww  .  j  av  a  2 s.  c  o  m*/
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Eligir ubicacin de la base de datos.");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            txtDatadir.setText(chooser.getSelectedFile().getPath());
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null,
                Thread.currentThread().getStackTrace()[1].getMethodName() + " - " + ex.getMessage());
        LOGGER.error(Thread.currentThread().getStackTrace()[1].getMethodName(), ex);
    }
}

From source file:com.igormaznitsa.sciareto.ui.MainFrame.java

private void menuNewProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewProjectActionPerformed
    final JFileChooser folder = new JFileChooser();
    folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    folder.setDialogTitle("Create project folder");
    folder.setApproveButtonText("Create");
    folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (folder.showOpenDialog(Main.getApplicationFrame()) == JFileChooser.APPROVE_OPTION) {
        final File file = folder.getSelectedFile();
        if (file.exists()) {
            DialogProviderManager.getInstance().getDialogProvider()
                    .msgError("File '" + file.getName() + "' already exists!");
        } else if (file.mkdirs()) {
            if (PreferencesManager.getInstance().getPreferences()
                    .getBoolean(PreferencesPanel.PREFERENCE_KEY_KNOWLEDGEFOLDER_ALLOWED, true)) {
                final File knowledgeFolder = new File(file, ".projectKnowledge");
                knowledgeFolder.mkdirs();
            }//from   w w w  .  j  a v a  2s .c  o m
            if (openProject(file, true)) {
                this.focusInTree(file);
            }
        } else {
            LOGGER.error("Can't create folder : " + file);
            DialogProviderManager.getInstance().getDialogProvider().msgError("Can't create folder: " + file);
        }
    }
}

From source file:com.opendoorlogistics.studio.appframe.AppFrame.java

@Override
public void openDatastoreWithUserPrompt() {
    if (!canCloseDatastore()) {
        return;//from  ww w .  ja  va 2  s .c o m

    }
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(ImportFileType.EXCEL.getFilter());

    File defaultDir = PreferencesManager.getSingleton().getFile(PrefKey.LAST_IO_DIR);
    if (defaultDir != null) {
        IOUtils.setFile(defaultDir, chooser);
    }

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

        openFile(file);
    }
    updateAppearance();
}

From source file:io.heming.accountbook.ui.MainFrame.java

private void importRecords() {
    final JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.addChoosableFileFilter(new AbbFileFilter());
    chooser.setAcceptAllFileFilterUsed(false);
    int value = chooser.showOpenDialog(this);
    if (value == JFileChooser.APPROVE_OPTION) {
        // ?//from w w w . ja v  a  2 s .  co  m
        disableAllControls();
        new Thread() {
            @Override
            public void run() {
                try {
                    statusLabel.setIcon(new ImageIcon(getClass().getResource("loader.gif")));
                    setStatusText("?...");
                    FacadeUtil.shutdown();
                    File file = chooser.getSelectedFile();
                    setStatusText("??...");
                    FileUtils.cleanDirectory(new File(Constants.DATA_DIR));
                    setStatusText("??...");
                    ZIPUtil.decompress(file, new File(Constants.HOME_DIR));
                    setStatusText("?...");
                    FacadeUtil.restart();
                    // ??
                    setStatusText("????...");
                    categories = categoryFacade.listBySale();
                    // ????
                    setStatusText("?...");
                    searchRecords();
                    enableAllControls();
                    setStatusText("");
                    statusLabel.setIcon(new ImageIcon(getClass().getResource("stopped-loader.png")));
                    JOptionPane.showMessageDialog(MainFrame.this, "???", "?",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(), "",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }.start();
    }
}

From source file:techtonic.Onview.java

private void jmiLoadActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter fnef = new FileNameExtensionFilter("xml file only", ".xml");
    chooser.setFileFilter(fnef);/*w  w w .  j a  v a2  s.co m*/
    int input = chooser.showOpenDialog(this);
    if (input == 0) {

    }
}

From source file:net.itransformers.topologyviewer.gui.GraphViewerPanel.java

private JButton createLoadButton() {
    JButton load = new JButton("Load");
    load.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            PersistentLayout pl = (PersistentLayout) vv.getGraphLayout();
            try {
                JFileChooser chooser = new JFileChooser(currentDir);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.setMultiSelectionEnabled(false);
                chooser.setFileFilter(new LayoutFileFilter());
                int result = chooser.showOpenDialog(GraphViewerPanel.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    currentDir = chooser.getCurrentDirectory();
                    String absolutePath = chooser.getSelectedFile().getAbsolutePath();
                    if (!absolutePath.endsWith(".layout")) {
                        absolutePath += ".layout";
                    }/* w ww . ja va 2 s . c o  m*/
                    pl.restore(absolutePath);
                    vv.repaint();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(GraphViewerPanel.this,
                        "Error restoring layout: " + e1.getMessage());
            }
        }
    });
    return load;
}

From source file:ch.fork.AdHocRailway.ui.locomotives.configuration.LocomotiveConfig.java

public void chooseLocoImage() {
    File previousLocoDir = ctx.getPreviousLocoDir();
    if (previousLocoDir == null) {
        previousLocoDir = new File("locoimages");
    }/*from  w w  w. j a va2s  .  c om*/
    final JFileChooser chooser = new JFileChooser(previousLocoDir);

    final ImagePreviewPanel preview = new ImagePreviewPanel();
    chooser.setAccessory(preview);
    chooser.addPropertyChangeListener(preview);

    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Image Files";
        }

        @Override
        public boolean accept(final File f) {
            if (f.isDirectory()) {
                return true;
            }
            if (StringUtils.endsWithAny(f.getName().toLowerCase(), ".png", ".gif", ".bmp", ".jpg")) {
                return true;
            }
            return false;
        }
    });

    final int ret = chooser.showOpenDialog(LocomotiveConfig.this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File selectedFile = chooser.getSelectedFile();
        ctx.setPreviousLocoDir(selectedFile.getParentFile());
        presentationModel.getBean().setImage(selectedFile.getName());
        final String image = presentationModel.getBean().getImage();
        presentationModel.getBean()
                .setImageBase64(LocomotiveImageHelper.getImageBase64(presentationModel.getBean()));
        if (image != null && !image.isEmpty()) {
            imageLabel.setIcon(LocomotiveImageHelper.getLocomotiveIcon(presentationModel.getBean()));
            pack();
        } else {
            imageLabel.setIcon(null);
            pack();
        }
    }
}

From source file:edu.harvard.mcz.imagecapture.jobs.JobRecheckForTemplates.java

private List<ICImage> getFileList() {
    List<ICImage> files = new ArrayList<ICImage>();
    if (scan != SCAN_ALL) {
        String pathToCheck = "";
        // Find the path in which to include files.
        File imagebase = null; // place to start the scan from, imagebase directory for SCAN_ALL
        File startPoint = null;// www .ja  va2  s  . c o  m
        // If it isn't null, retrieve the image base directory from properties, and test for read access.
        if (Singleton.getSingletonInstance().getProperties().getProperties()
                .getProperty(ImageCaptureProperties.KEY_IMAGEBASE) == null) {
            JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                    "Can't start scan.  Don't know where images are stored.  Set imagbase property.",
                    "Can't Scan.", JOptionPane.ERROR_MESSAGE);
        } else {
            imagebase = new File(Singleton.getSingletonInstance().getProperties().getProperties()
                    .getProperty(ImageCaptureProperties.KEY_IMAGEBASE));
            if (imagebase != null) {
                if (imagebase.canRead()) {
                    startPoint = imagebase;
                } else {
                    // If it can't be read, null out imagebase
                    imagebase = null;
                }
            }
            if (scan == SCAN_SPECIFIC && startPointSpecific != null && startPointSpecific.canRead()) {
                // A scan start point has been provided, don't launch a dialog.
                startPoint = startPointSpecific;
            }
            if (imagebase == null || scan == SCAN_SELECT) {
                // launch a file chooser dialog to select the directory to scan
                final JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (scan == SCAN_SELECT && startPointSpecific != null && startPointSpecific.canRead()) {
                    fileChooser.setCurrentDirectory(startPointSpecific);
                } else {
                    if (Singleton.getSingletonInstance().getProperties().getProperties()
                            .getProperty(ImageCaptureProperties.KEY_LASTPATH) != null) {
                        fileChooser
                                .setCurrentDirectory(new File(Singleton.getSingletonInstance().getProperties()
                                        .getProperties().getProperty(ImageCaptureProperties.KEY_LASTPATH)));
                    }
                }
                int returnValue = fileChooser.showOpenDialog(Singleton.getSingletonInstance().getMainFrame());
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    log.debug("Selected base directory: " + file.getName() + ".");
                    startPoint = file;
                } else {
                    //TODO: handle error condition
                    log.error("Directory selection cancelled by user.");
                }
            }

            // Check that startPoint is or is within imagebase.
            if (!ImageCaptureProperties.isInPathBelowBase(startPoint)) {
                String base = Singleton.getSingletonInstance().getProperties().getProperties()
                        .getProperty(ImageCaptureProperties.KEY_IMAGEBASE);
                log.error("Tried to scan directory (" + startPoint.getPath()
                        + ") outside of base image directory (" + base + ")");
                String message = "Can't scan and database files outside of base image directory (" + base + ")";
                JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), message,
                        "Can't Scan outside image base directory.", JOptionPane.YES_NO_OPTION);
            } else {
                if (!startPoint.canRead()) {
                    JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                            "Can't start scan.  Unable to read selected directory: " + startPoint.getPath(),
                            "Can't Scan.", JOptionPane.YES_NO_OPTION);
                } else {
                    pathToCheck = ImageCaptureProperties.getPathBelowBase(startPoint);

                    // retrieve a list of image records in the selected directory
                    ICImageLifeCycle ils = new ICImageLifeCycle();
                    ICImage pattern = new ICImage();
                    pattern.setPath(pathToCheck);
                    pattern.setTemplateId(PositionTemplate.TEMPLATE_NO_COMPONENT_PARTS);
                    files = ils.findByExample(pattern);
                    if (files != null) {
                        log.debug(files.size());
                    }
                }
            }
        }
    } else {
        try {
            // retrieve a list of all image records with no template
            ICImageLifeCycle ils = new ICImageLifeCycle();
            files = ils.findNotDrawerNoTemplateImages();
            if (files != null) {
                log.debug(files.size());
            }
        } catch (HibernateException e) {
            log.error(e.getMessage());
            runStatus = RunStatus.STATUS_FAILED;
            String message = "Error loading the list of images with no templates. " + e.getMessage();
            JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), message,
                    "Error loading image records.", JOptionPane.YES_NO_OPTION);
        }
    }

    log.debug("Found " + files.size() + " Image files without templates in directory to check.");

    return files;
}

From source file:net.sourceforge.doddle_owl.ui.InputDocumentSelectionPanel.java

private Set getFiles() {
    JFileChooser chooser = new JFileChooser(DODDLEConstants.PROJECT_HOME);
    chooser.setMultiSelectionEnabled(true);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int retval = chooser.showOpenDialog(DODDLE_OWL.rootPane);
    if (retval != JFileChooser.APPROVE_OPTION) {
        return null;
    }/*  w  w w . j  a  v a2  s.  co m*/
    File[] files = chooser.getSelectedFiles();
    Set fileSet = new TreeSet();
    getFiles(files, fileSet);
    return fileSet;
}