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:archive_v1.Archive_Form.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    CreateConnection();/*from w ww.  j  av  a  2 s . com*/
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int a = jfc.showOpenDialog(this);
    if (a == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        try {
            // What to do with the file, e.g. display it in a TextArea
            System.out.println(file.getAbsolutePath());
        } catch (Exception ex) {
            System.out.println("problem accessing file" + file.getAbsolutePath());
        }
        File[] folder = new File(file.getAbsolutePath()).listFiles();
        for (File vid : folder) {
            filename = vid.getName();
            size = getFileSize(file.getAbsolutePath() + "\\" + filename);
            tape_id = generateTapeId();
            insertArchive(filename, size, tape_id, username);

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:com.naval.gui.Gui.java

private void chargerPartie() {
    final JFileChooser fc = new JFileChooser(Config.getRepTravail());

    fc.addChoosableFileFilter(new GameFileFilter("serial", "Sauv de partie"));
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(frame);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        // TODO : load only if game is !=
        try {//w  w  w.  j a  va2 s.  co  m
            partie = Partie.load(file.getAbsolutePath());

            menuFac.updateForLoad();
            update();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            hintBar.setText(e.getMessage());
        } catch (IOException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            hintBar.setText(e.getMessage());
            e.printStackTrace();
        }
    }
    if (partie.ordres == null || partie.ordres.size() == 0) {
        for (Navire n : partie.navires) {
            // creation des 3 ordres pour le tour courant.
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));
            partie.ordres.add(new Ordre(n.id, partie.minute));

        }
    }
}

From source file:TextFileHandler.java

public File openDirectory(String title) {
    File result = null;// w ww. j  a va  2s . co  m
    JFileChooser chooser = new JFileChooser(new File("."));
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (title != null)
        chooser.setDialogTitle(title);
    int retVal = chooser.showOpenDialog(null);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        result = chooser.getSelectedFile();
    }
    return result;
}

From source file:com.itd.dbmrgdao.TestTime_newscanner.java

@Ignore
@org.junit.Test/* w  w  w.j a v a  2s  .  c o  m*/
public void test() throws ParseException {

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    String fileName = f.getAbsolutePath();
    try {
        FileReader reader = new FileReader(fileName);
        BufferedReader br = new BufferedReader(reader);
        String strLine;

        Boolean result = updateScanFileRoundUp2(br);

        br.close();

    } catch (IOException ex) {

    }

}

From source file:net.sf.maltcms.chromaui.ui.PaintScalePanel.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    JFileChooser jfc = new JFileChooser();
    int ret = jfc.showOpenDialog(getParent());
    if (ret == JFileChooser.APPROVE_OPTION) {
        File f = jfc.getSelectedFile();
        GradientPaintScale gps = new GradientPaintScale(getSampleTable(samples), 0, 1,
                ImageTools.rampToColorArray(new ColorRampReader().readColorRamp(f.getAbsolutePath())));
        gps.setLabel(f.getName());//  w w w .  ja v  a2s  .  c om
        dcbm.addElement(gps);
        dcbm.setSelectedItem(gps);
    }
}

From source file:fll.scheduler.ChooseChallengeDescriptor.java

private void initComponents() {
    getContentPane().setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.weightx = 1;/*w w  w  . j  ava  2 s. co  m*/
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    final JTextArea instructions = new JTextArea(
            "Choose a challenge description from the drop down list OR choose a file containing your custom challenge description.",
            3, 40);
    instructions.setEditable(false);
    instructions.setWrapStyleWord(true);
    instructions.setLineWrap(true);
    getContentPane().add(instructions, gbc);

    gbc = new GridBagConstraints();
    mCombo = new JComboBox<DescriptionInfo>();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    getContentPane().add(mCombo, gbc);
    mCombo.setRenderer(new DescriptionInfoRenderer());
    mCombo.setEditable(false);
    final List<DescriptionInfo> descriptions = DescriptionInfo.getAllKnownChallengeDescriptionInfo();
    for (final DescriptionInfo info : descriptions) {
        mCombo.addItem(info);
    }

    mFileField = new JLabel();
    gbc = new GridBagConstraints();
    gbc.weightx = 1;
    getContentPane().add(mFileField, gbc);

    final JButton chooseButton = new JButton("Choose File");
    gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    getContentPane().add(chooseButton, gbc);
    chooseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent ae) {
            mFileField.setText(null);

            final JFileChooser fileChooser = new JFileChooser();
            final FileFilter filter = new BasicFileFilter("FLL Description (xml)", new String[] { "xml" });
            fileChooser.setFileFilter(filter);

            final int returnVal = fileChooser.showOpenDialog(ChooseChallengeDescriptor.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                final File selectedFile = fileChooser.getSelectedFile();
                mFileField.setText(selectedFile.getAbsolutePath());
            }
        }
    });

    final Box buttonPanel = new Box(BoxLayout.X_AXIS);
    gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    getContentPane().add(buttonPanel, gbc);

    buttonPanel.add(Box.createHorizontalGlue());
    final JButton ok = new JButton("Ok");
    buttonPanel.add(ok);
    ok.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent ae) {

            // use the selected description if nothing is entered in the file box
            final DescriptionInfo descriptionInfo = mCombo.getItemAt(mCombo.getSelectedIndex());
            if (null != descriptionInfo) {
                mSelected = descriptionInfo.getURL();
            }

            final String text = mFileField.getText();
            if (!StringUtils.isEmpty(text)) {
                final File file = new File(text);
                if (file.exists()) {
                    try {
                        mSelected = file.toURI().toURL();
                    } catch (final MalformedURLException e) {
                        throw new FLLInternalException("Can't turn file into URL?", e);
                    }
                }
            }

            setVisible(false);
        }
    });

    final JButton cancel = new JButton("Cancel");
    buttonPanel.add(cancel);
    cancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent ae) {
            mSelected = null;
            setVisible(false);
        }
    });

    pack();
}

From source file:com.db4o.sync4o.ui.Db4oSyncSourceConfigPanel.java

/**
 * Default constructor is required by Funambol Admin UI
 *///from  w w w. j a  va 2  s  . c  o  m
public Db4oSyncSourceConfigPanel() {

    setupControls();

    // now we install our event handlers and we are ready to go...

    // handler to update the SyncClassConfigTree upon a change 
    // in database file
    _dbFileLocateButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            try {

                final JFileChooser fc = new JFileChooser();
                int rc = fc.showOpenDialog(Db4oSyncSourceConfigPanel.this);
                if (JFileChooser.APPROVE_OPTION == rc) {

                    File f = fc.getSelectedFile();
                    _dbFileValue.setText(f.getPath());
                    Db4oSyncSourceConfigPanel.this.refreshClassConfigsFromFile();

                }

            } catch (Exception ex) {

                StringWriter s = new StringWriter();
                PrintWriter w = new PrintWriter(s);

                ex.printStackTrace(w);
                notifyError(new AdminException(s.toString()));

            }

        }

    });

    // handler for the "Add" or "Update" button
    _confirmButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {

            try {
                validateValues();
                updateSyncSource();

                if (getState() == STATE_INSERT) {
                    Db4oSyncSourceConfigPanel.this.actionPerformed(new ActionEvent(
                            Db4oSyncSourceConfigPanel.this, ACTION_EVENT_INSERT, event.getActionCommand()));
                } else {
                    Db4oSyncSourceConfigPanel.this.actionPerformed(new ActionEvent(
                            Db4oSyncSourceConfigPanel.this, ACTION_EVENT_UPDATE, event.getActionCommand()));
                }
            } catch (Exception e) {
                notifyError(new AdminException(e.getMessage()));
            }
        }
    });

}

From source file:com.original.widget.OPicture.java

/**
 * very simple method to select an image file.
 * Please be advised that we will create our own file chooser in the
 * near future. Since in our system, people will be not encouraged to
 * access the physical file.//w  w  w.ja  v  a 2s  .c  o  m
 *
 * Of course, we need to discuss how to handle this issue.
 * @return
 */

private File chooseImgFile() {
    JFileChooser chooser = new JFileChooser(".");
    FileFilter imgType = new OriExtFileFilter("Image files", new String[] { ".jpg", ".gif", ".jpeg", ".png" });
    chooser.addChoosableFileFilter(imgType);
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileFilter(imgType);
    int status = chooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
        File f = chooser.getSelectedFile();
        return f;
    }
    return null;
}

From source file:it.isislab.dmason.util.SystemManagement.Worker.WorkerUpdater.java

public File showFileChooser() {
    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setCurrentDirectory(new File(FTP_HOME));
    int n = fileChooser.showOpenDialog(WorkerUpdater.this);
    if (n == JFileChooser.APPROVE_OPTION) {
        return fileChooser.getSelectedFile();
    } else/*from   w w  w.jav a 2  s. co  m*/
        return null;
}

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

private void importarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importarActionPerformed
    try {/*from   w  w w .j  a  va  2 s  .c  om*/
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Hojas de clculo", "xlsx");
        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));
            this.application.importarArchivo(chooser.getSelectedFile());
            this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
        }
    } catch (IOException ex) {
        FormatoProgramacionView.LOGGER.error(ex);
    }
}