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._17od.upm.gui.OptionsDialog.java

private void getDBToLoadOnStartup() {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.translate("dbToOpenOnStartup"));
    int returnVal = fc.showOpenDialog(parentFrame);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File databaseFile = fc.getSelectedFile();
        dbToLoadOnStartup.setText(databaseFile.getAbsoluteFile().toString());
    }/*from  w  w  w.j  a  va2  s .  c om*/
}

From source file:com.akman.excel.view.frmSelectImage.java

private void btnSelectImageActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSelectImageActionPerformed
{//GEN-HEADEREND:event_btnSelectImageActionPerformed
    JFileChooser chooser = new JFileChooser();

    FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "png", "gif", "bmp");
    chooser.setFileFilter(filter);/*from   w  ww.  j a  v  a2  s.  c o m*/

    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle("Select The Image");
    chooser.setMultiSelectionEnabled(false);
    int res = chooser.showOpenDialog(null);
    if (res == JFileChooser.APPROVE_OPTION) {

        //Saving file inside the file
        File file = chooser.getSelectedFile();

        //        if(!file.equals(filter))
        //        {
        //        JOptionPane.showMessageDialog(null, "Wrong File Selected","ERROR",JOptionPane.ERROR_MESSAGE);
        //        return;
        //        }

        //System.out.println(file.getAbsolutePath());
        ImageIcon image = new ImageIcon(file.getAbsolutePath());

        fileName = file.getAbsolutePath();

        // Get Width And Height of PicLabel
        Rectangle rect = lblImage.getBounds();

        //System.out.println(lblImage.getBounds());
        //Scaling the image to fit in the picLabel
        Image scaledimage = image.getImage().getScaledInstance(rect.width, rect.height, Image.SCALE_DEFAULT);
        //converting the image back to image icon to make an acceptable picLabel
        image = new ImageIcon(scaledimage);

        lblImage.setIcon(image);

        txtPath.setText(fileName);

        try {

            File images = new File(fileName);

            FileInputStream fis = new FileInputStream(images);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }

            person_image = bos.toByteArray();

        } catch (Exception e) {

            JOptionPane.showMessageDialog(null, e);
        }

    }
}

From source file:com.enderville.enderinstaller.ui.Installer.java

private void chooseTargetMinecraftFolder() {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    int opt = chooser.showOpenDialog(getMainPane());
    if (opt == JFileChooser.APPROVE_OPTION) {
        File dir = chooser.getSelectedFile();
        String oldDir = InstallerConfig.getMinecraftFolder();
        InstallerConfig.setMinecraftFolder(dir.getAbsolutePath());
        File mcjar = new File(InstallerConfig.getMinecraftJar());
        if (!mcjar.exists()) {
            JOptionPane.showMessageDialog(getMainPane(),
                    "The installer couldn't find a minecraft installation in the specified folder.\n"
                            + "Restoring minecraft folder to " + oldDir,
                    "Error setting target Minecraft installation", JOptionPane.ERROR_MESSAGE);
            InstallerConfig.setMinecraftFolder(oldDir);
        }// w  w  w. j a v  a 2  s. c o  m
    }

}

From source file:be.fedict.eid.tsl.tool.TslTool.java

@Override
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (EXIT_ACTION_COMMAND.equals(command)) {
        System.exit(0);//from w ww . j  ava2  s .  co  m
    } else if (OPEN_ACTION_COMMAND.equals(command)) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Open TSL");
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            displayTsl(fileChooser.getSelectedFile());
        }
    } else if (ABOUT_ACTION_COMMAND.equals(command)) {
        JOptionPane.showMessageDialog(this,
                "eID TSL Tool\n" + "Copyright (C) 2009-2013 FedICT\n" + "http://code.google.com/p/eid-tsl/",
                "About", JOptionPane.INFORMATION_MESSAGE);
    } else if (CLOSE_ACTION_COMMAND.equals(command)) {
        if (this.activeTslInternalFrame.getTrustServiceList().hasChanged()) {
            int result = JOptionPane.showConfirmDialog(this, "TSL has been changed.\n" + "Save the TSL?",
                    "Save", JOptionPane.YES_NO_CANCEL_OPTION);
            if (JOptionPane.CANCEL_OPTION == result) {
                return;
            }
            if (JOptionPane.YES_OPTION == result) {
                try {
                    this.activeTslInternalFrame.save();
                } catch (IOException e) {
                    LOG.error("IO error: " + e.getMessage(), e);
                }
            }
        }
        try {
            this.activeTslInternalFrame.setClosed(true);
        } catch (PropertyVetoException e) {
            LOG.warn("property veto error: " + e.getMessage(), e);
        }
    } else if (SIGN_ACTION_COMMAND.equals(command)) {
        LOG.debug("sign");
        TrustServiceList trustServiceList = this.activeTslInternalFrame.getTrustServiceList();
        if (trustServiceList.hasSignature()) {
            int confirmResult = JOptionPane.showConfirmDialog(this,
                    "TSL is already signed.\n" + "Resign the TSL?", "Resign", JOptionPane.OK_CANCEL_OPTION);
            if (JOptionPane.CANCEL_OPTION == confirmResult) {
                return;
            }
        }
        SignSelectPkcs11FinishablePanel pkcs11Panel = new SignSelectPkcs11FinishablePanel();
        WizardDescriptor wizardDescriptor = new WizardDescriptor(
                new WizardDescriptor.Panel[] { new SignInitFinishablePanel(), pkcs11Panel,
                        new SignSelectCertificatePanel(pkcs11Panel, trustServiceList),
                        new SignFinishFinishablePanel() });
        wizardDescriptor.setTitle("Sign TSL");
        wizardDescriptor.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
        DialogDisplayer dialogDisplayer = DialogDisplayer.getDefault();
        Dialog wizardDialog = dialogDisplayer.createDialog(wizardDescriptor);
        wizardDialog.setVisible(true);
    } else if (SAVE_ACTION_COMMAND.equals(command)) {
        LOG.debug("save");
        try {
            this.activeTslInternalFrame.save();
            this.saveMenuItem.setEnabled(false);
        } catch (IOException e) {
            LOG.debug("IO error: " + e.getMessage(), e);
        }
    } else if (SAVE_AS_ACTION_COMMAND.equals(command)) {
        LOG.debug("save as");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Save As");
        int result = fileChooser.showSaveDialog(this);
        if (JFileChooser.APPROVE_OPTION == result) {
            File tslFile = fileChooser.getSelectedFile();
            if (tslFile.exists()) {
                int confirmResult = JOptionPane.showConfirmDialog(this,
                        "File already exists.\n" + tslFile.getAbsolutePath() + "\n" + "Overwrite file?",
                        "Overwrite", JOptionPane.OK_CANCEL_OPTION);
                if (JOptionPane.CANCEL_OPTION == confirmResult) {
                    return;
                }
            }
            try {
                this.activeTslInternalFrame.saveAs(tslFile);
            } catch (IOException e) {
                LOG.debug("IO error: " + e.getMessage(), e);
            }
            this.saveMenuItem.setEnabled(false);
        }
    } else if (EXPORT_ACTION_COMMAND.equals(command)) {
        LOG.debug("export");
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Export to PDF");
        int result = fileChooser.showSaveDialog(this);
        if (JFileChooser.APPROVE_OPTION == result) {
            File pdfFile = fileChooser.getSelectedFile();
            if (pdfFile.exists()) {
                int confirmResult = JOptionPane.showConfirmDialog(this,
                        "File already exists.\n" + pdfFile.getAbsolutePath() + "\n" + "Overwrite file?",
                        "Overwrite", JOptionPane.OK_CANCEL_OPTION);
                if (JOptionPane.CANCEL_OPTION == confirmResult) {
                    return;
                }
            }
            try {
                this.activeTslInternalFrame.export(pdfFile);
            } catch (IOException e) {
                LOG.debug("IO error: " + e.getMessage(), e);
            }
        }
    } else if ("TSL-BE-2010-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2010, Trimester.FIRST);
        displayTsl("*TSL-BE-2010-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2010-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2010, Trimester.SECOND);
        displayTsl("*TSL-BE-2010-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2010-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2010, Trimester.THIRD);
        displayTsl("*TSL-BE-2010-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2011-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2011, Trimester.FIRST);
        displayTsl("*TSL-BE-2011-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2011-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2011, Trimester.SECOND);
        displayTsl("*TSL-BE-2011-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2011-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2011, Trimester.THIRD);
        displayTsl("*TSL-BE-2011-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2012-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2012, Trimester.FIRST);
        displayTsl("*TSL-BE-2012-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2012-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2012, Trimester.SECOND);
        displayTsl("*TSL-BE-2012-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2012-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2012, Trimester.THIRD);
        displayTsl("*TSL-BE-2012-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2013-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2013, Trimester.FIRST);
        displayTsl("*TSL-BE-2013-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2013-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2013, Trimester.SECOND);
        displayTsl("*TSL-BE-2013-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2013-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2013, Trimester.THIRD);
        displayTsl("*TSL-BE-2013-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2014-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2014, Trimester.FIRST);
        displayTsl("*TSL-BE-2014-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2014-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2014, Trimester.SECOND);
        displayTsl("*TSL-BE-2014-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2014-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2014, Trimester.THIRD);
        displayTsl("*TSL-BE-2014-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2015-T1".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2015, Trimester.FIRST);
        displayTsl("*TSL-BE-2015-T1.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2015-T2".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2015, Trimester.SECOND);
        displayTsl("*TSL-BE-2015-T2.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    } else if ("TSL-BE-2015-T3".equals(command)) {
        TrustServiceList trustServiceList = BelgianTrustServiceListFactory.newInstance(2015, Trimester.THIRD);
        displayTsl("*TSL-BE-2015-T3.xml", trustServiceList);
        this.saveMenuItem.setEnabled(false);
    }
}

From source file:baocaoxla.compare.java

private void jMenuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuOpenActionPerformed
    JFileChooser fc = new JFileChooser();
    int f = fc.showOpenDialog(this);
    if (f == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        try {//w  ww. ja  va  2  s  .c o m
            img = ImageIO.read(new File(file.getPath()));
            ImageIcon icon = new ImageIcon(
                    img.getScaledInstance(lbimage.getWidth(), lbimage.getHeight(), Image.SCALE_SMOOTH));
            lbimage.setIcon(icon);
            pnimagehistogram.removeAll();
            pnimagehistogram.add(xuly.displayhistogram(img));
            pnimagehistogram.validate();
        } catch (IOException ex) {
            Logger.getLogger(compare.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (jcbtwo.isSelected()) {
            fc.showOpenDialog(this);
            file = fc.getSelectedFile();
            try {
                imgcompare = ImageIO.read(new File(file.getPath()));
                ImageIcon icon1 = new ImageIcon(imgcompare.getScaledInstance(lbimagecompare.getWidth(),
                        lbimagecompare.getHeight(), Image.SCALE_SMOOTH));
                lbimagecompare.setIcon(icon1);
                pnimagehistogram1.removeAll();
                pnimagehistogram1.add(xuly.displayhistogram(imgcompare));
                pnimagehistogram1.validate();
            } catch (IOException ex) {
                Logger.getLogger(compare.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}

From source file:be.ugent.maf.cellmissy.gui.controller.TracksWriterController.java

/**
 * Choose Directory//  w  w  w  . j  a  va2s . c om
 */
private void chooseDirectory() {
    // Open a JFile Chooser
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Select directory to save the files");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setAcceptAllFileFilterUsed(false);
    // in response to the button click, show open dialog
    int returnVal = fileChooser.showOpenDialog(tracksWriterDialog);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        directory = fileChooser.getSelectedFile();
    }
    tracksWriterDialog.getDirectoryTextField().setText(directory.getAbsolutePath());
}

From source file:br.upe.ecomp.dosa.view.mainwindow.MainWindowActions.java

private boolean chooseDirectory(JTextField textField) {
    JFileChooser fileOpen = new JFileChooser();
    fileOpen.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileOpen.showOpenDialog(this);

    boolean directoryChoosed = false;

    if (fileOpen.getSelectedFile() != null) {
        File directory = new File(fileOpen.getSelectedFile().getAbsolutePath());
        textField.setText(directory.getPath());

        resultFiles = getFilesOnDirectory(directory);
        directoryChoosed = true;//from  ww w  .j  a  v a 2 s  .c o m
    }

    // Codigo para Mac OSX
    // FileDialog fileopen = new FileDialog(this, "Open Results Directory", FileDialog.LOAD);
    // fileopen.setModalityType(ModalityType.DOCUMENT_MODAL);
    //
    // System.setProperty("apple.awt.fileDialogForDirectories", "true");
    // fileopen.setVisible(true);
    // System.setProperty("apple.awt.fileDialogForDirectories", "false");
    //
    // if (fileopen.getFile() != null) {
    // File directory = new File(fileopen.getDirectory(), fileopen.getFile());
    // textField.setText(directory.getPath());
    //
    // resultFiles = getFilesOnDirectory(directory);
    // // List<File> foundFiles = ;
    // // for (File file : foundFiles) {
    // // if (file.getName().endsWith(".txt")) {
    // // resultFiles.add(file);
    // // }
    // // }
    // directoryChoosed = true;
    // }
    return directoryChoosed;
}

From source file:org.nekorp.workflow.desktop.view.EvidenciaEventoView.java

private void nuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevaActionPerformed
    try {/*w w w . java2s  . c  om*/
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Imagen", "jpg", "jpeg", "png");
        chooser.setFileFilter(filter);
        String homePath = System.getProperty("user.home");
        File f = new File(new File(homePath).getCanonicalPath());
        chooser.setSelectedFile(f);
        int returnVal = chooser.showOpenDialog(this.mainFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            BufferedImage img = ImageIO.read(chooser.getSelectedFile());
            img = imageService.resizeToStandarSize(img);
            BufferedImage thumb = imageService.getThumbnail(img);
            EvidenciaVB nuevaEvidencia = new EvidenciaVB();
            nuevaEvidencia.setImage(imageService.guardarImagen(img));
            nuevaEvidencia.setThumbnail(imageService.guardarImagen(thumb));
            ThumbnailView thumbview = new ThumbnailView(thumb, this);
            thumbview.setEditableStatus(editable);
            thumbs.add(thumbview);
            modelo.add(nuevaEvidencia);
            previewContent.add(thumbview);
            this.ignore.add(modelo);
            actualizaModelo();
            selectEvent(thumbview);
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
        }
    } catch (IOException ex) {
        EvidenciaEventoView.LOGGER.error("Error al seleccionar archivo de imagen", ex);
    }
}

From source file:jpad.MainEditor.java

public void openFile() {
    if (_isOSX || ostype == ostype.Linux || ostype == ostype.Other) {
        openFile_OSX_Nix();/*w  w w  .  j  a  v a  2  s  .c om*/
    } else {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Open a Text File");
        fc.setFileFilter(new FileNameExtensionFilter("Plain Text Files", "txt"));
        int returnval = fc.showOpenDialog(this);
        if (returnval == 0) {
            hasSavedToFile = true;
            hasChanges = false;
            curFile = fc.getSelectedFile().getAbsolutePath();
            try (FileInputStream stream = new FileInputStream(curFile)) {
                String file = IOUtils.toString(stream);
                mainTextArea.setText(file);
                this.setTitle(String.format("JPad - %s", curFile));
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:XPathTest.java

/**
     * Open a file and load the document.
     *//*from  w w  w .  j  a v  a  2s . c o  m*/
    public void openFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
            }

            public String getDescription() {
                return "XML files";
            }
        });
        int r = chooser.showOpenDialog(this);
        if (r != JFileChooser.APPROVE_OPTION)
            return;
        File f = chooser.getSelectedFile();
        try {
            byte[] bytes = new byte[(int) f.length()];
            new FileInputStream(f).read(bytes);
            docText.setText(new String(bytes));
            doc = builder.parse(f);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, e);
        } catch (SAXException e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }