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.haulmont.cuba.desktop.gui.components.DesktopFileMultiUploadField.java

public DesktopFileMultiUploadField() {
    fileUploading = AppBeans.get(FileUploadingAPI.NAME);

    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setMultiSelectionEnabled(true);

    DesktopResources resources = App.getInstance().getResources();
    Messages messages = AppBeans.get(Messages.NAME);
    String caption = messages.getMessage(getClass(), "upload.selectFiles");
    impl = new JButton();
    impl.setAction(new AbstractAction(caption, resources.getIcon(DEFAULT_ICON)) {
        @Override// w  w w . j ava  2 s  .  c o m
        public void actionPerformed(ActionEvent e) {
            if (fileChooser.showOpenDialog(impl) == JFileChooser.APPROVE_OPTION) {
                processFiles(fileChooser.getSelectedFiles());
            }
        }
    });
    DesktopComponentsHelper.adjustSize(impl);
}

From source file:com.apatar.ui.JPublishToApatarDialog.java

private void createListeners() {
    browse.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
            fileChooser.setMultiSelectionEnabled(false);

            int returnValue = fileChooser.showOpenDialog(getContentPane());
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File fileSrc = fileChooser.getSelectedFile();
                nameFile.setText(fileSrc.getPath());
            }//from   www  .ja va2 s . c om
        }

    });

    bCancel.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            option = CANCEL_OPTION;
            setVisible(false);
        }

    });

    bOk.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            option = OK_OPTION;
            setVisible(false);
        }

    });

    selectFromFile.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            if (selectFromFile.isSelected()) {
                nameFile.setEnabled(true);
                browse.setEnabled(true);
            } else {
                nameFile.setEnabled(false);
                browse.setEnabled(false);
            }
        }
    });

    autoGenerateShortDescription.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            if (autoGenerateShortDescription.isSelected())
                setEnableShortDescription(false);
            else
                setEnableShortDescription(true);
        }
    });
}

From source file:maltcms.ui.nb.pipelineRunner.options.LocalMaltcmsExecutionPanel.java

private void selectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectActionPerformed
    FileChooserBuilder fcb = new FileChooserBuilder(LocalMaltcmsExecutionPanel.class);
    fcb.setFilesOnly(false);//from   w w  w. j  a va  2 s. co  m
    fcb.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }
            return file.getName().equals("maltcms.jar");
        }

        @Override
        public String getDescription() {
            return "maltcms.jar";
        }
    });
    fcb.setSelectionApprover(new FileChooserBuilder.SelectionApprover() {
        @Override
        public boolean approve(File[] files) {
            //                boolean approve = false;
            for (File f : files) {
                if (f.getName().equals("maltcms.jar")) {
                    return true;
                }
            }
            return false;
        }
    });
    JFileChooser jfc = fcb.createFileChooser();
    if (!maltcmsInstallationPath.getText().isEmpty()) {
        File basedir = new File(maltcmsInstallationPath.getText());
        jfc.setCurrentDirectory(basedir);
    }
    int ret = jfc.showOpenDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File f = jfc.getSelectedFile();
        maltcmsInstallationPath.setText(f.getParent());
        checkVersion(f.getParentFile().getAbsolutePath());
        controller.changed();
    }
}

From source file:androhashcheck.MainFrame.java

/**
 * Opens file expolorer to choose file to be uploaded.
 *
 * @param evt//from   w  w  w.ja v a 2  s. c om
 */
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        System.out.println("Selected file: " + selectedFile.getAbsolutePath());
        this.textField1.setText(selectedFile.getAbsolutePath());
    }
}

From source file:Forms.FrmPrincipal.java

private void btnFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFileActionPerformed
    // TODO add your handling code here:
    File sArchivo;//  w  w  w.j  a va 2s .  c o  m
    JFileChooser archivo = new JFileChooser();
    BufferedReader br = null;
    int lin = 0;
    patrones = new ArrayList<String[]>();

    try {
        archivo.setCurrentDirectory(new File(System.getProperty("user.home")));
        int result = archivo.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            sArchivo = archivo.getSelectedFile();
            txtArchivo.setText(sArchivo.getAbsolutePath());

            br = new BufferedReader(new FileReader(sArchivo.getAbsolutePath()));
            String linea = br.readLine();
            while (null != linea) {
                String[] campos = linea.split(SEPARADOR);
                numEntradas = campos.length;
                patrones.add(campos);
                linea = br.readLine();
                lin++;
            }
        }

        llenaMatriz();

    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (null != br)
            try {
                br.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
}

From source file:es.mityc.firmaJava.libreria.pkcs7.ValidaTarjeta.java

/**
 * This method initializes jExaminarButton   
 *    /*from  w  w w.  ja v a2s  .  c  om*/
 * @return javax.swing.JButton   
 */
private JButton getJExaminarButton() {
    if (jExaminarButton == null) {
        jExaminarButton = new JButton();
        jExaminarButton.setText(I18n.getResource(LIBRERIAXADES_VALIDARTARJETA_TEXTO_10));
        jExaminarButton.setPreferredSize(new Dimension(90, 20));
        jExaminarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final JFileChooser libreria = new JFileChooser();
                libreria.setDialogTitle(I18n.getResource(LIBRERIAXADES_VALIDARTARJETA_TEXTO_11));
                int returnVal = libreria.showOpenDialog(jPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    jLibreriaTextField.setText(libreria.getSelectedFile().toString());
                }
            }
        });
    }
    return jExaminarButton;
}

From source file:es.mityc.firmaJava.libreria.pkcs7.ValidaTarjeta.java

/**
 * This method initializes jExaminarButton1   
 *    //  ww  w .  ja v  a  2s .c om
 * @return javax.swing.JButton   
 */
private JButton getJExaminarButton1() {
    if (jExaminarButton1 == null) {
        jExaminarButton1 = new JButton();
        jExaminarButton1.setPreferredSize(new Dimension(90, 20));
        jExaminarButton1.setText(I18n.getResource(LIBRERIAXADES_VALIDARTARJETA_TEXTO_10));
        jExaminarButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final JFileChooser libreria = new JFileChooser();
                libreria.setDialogTitle(I18n.getResource(LIBRERIAXADES_VALIDARTARJETA_TEXTO_11));
                int returnVal = libreria.showOpenDialog(jPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    jNuevaLibreriaTextField.setText(libreria.getSelectedFile().toString());
                }
            }
        });
    }
    return jExaminarButton1;
}

From source file:graph.plotter.LineGraph.java

/**
 * This method works for choosing file path
            //from   w w w . j  av  a 2s.  c o  m
    */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    JFileChooser jf = new JFileChooser();
    jf.setBackground(Color.white);
    jf.showOpenDialog(null);
    File file = jf.getSelectedFile();
    String f = file.getAbsolutePath();
    jTextField4.setText(f);
}

From source file:com.signalcollect.sna.visualization.SignalCollectSNATopComponent.java

/**
 * Displays a file chooser when clicking on the "Choose File" button The
 * chosen file is then used for a new gephi project
 *
 * @param evt/*from  w ww  .j  a v  a 2s. c om*/
 */
private void fileChooserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooserButtonActionPerformed

    FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph Files", "gml");
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(filter);
    int result = chooser.showOpenDialog(this);
    if (chooser.getSelectedFile() != null && result == JFileChooser.APPROVE_OPTION) {
        fileName = chooser.getSelectedFile().getAbsolutePath();
        jTextArea1.setText(fileName);
        distributionFrame = new JFrame();

        try {
            ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
            pc.newProject();
            ImportController ic = Lookup.getDefault().lookup(ImportController.class);
            File file = new File(fileName);
            Container c = ic.importFile(file);
            ic.process(c, new DefaultProcessor(), pc.newWorkspace(pc.getCurrentProject()));
            propertyContentDisplay.setText("");

        } catch (Exception exception) {
            messageFrame = new JFrame();
            exception.printStackTrace();
            JOptionPane.showMessageDialog(messageFrame,
                    "Error while creating graph (" + exception.getCause()
                            + "), please choose a valid .gml-File",
                    "Signal/Collect Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:firmadigital.Firma.java

private void cmdExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExaminarActionPerformed
    JFileChooser fileChooser = new JFileChooser();

    txtUbicacion.setText("");
    lblNombreArchivo.setText("");
    lblRutaArchivo.setText("");

    String signFileName = txtUbicacion.getText();
    File directory = new File(signFileName).getParentFile();
    fileChooser.setCurrentDirectory(directory);
    FileNameExtensionFilter filter;
    filter = new FileNameExtensionFilter("XML file", "xml");
    fileChooser.setFileFilter(filter);/*from   w  w  w . ja v  a2  s.c o  m*/
    fileChooser.setFileHidingEnabled(true);
    /*Remove All File option*/
    fileChooser.setAcceptAllFileFilterUsed(false);

    if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        String selectedFile = fileChooser.getSelectedFile().getAbsolutePath();
        txtUbicacion.setText(selectedFile);
        lblNombreArchivo.setText(fileChooser.getSelectedFile().getName());
        lblRutaArchivo.setText(fileChooser.getSelectedFile().getParent());
    }
}