Java JFileChooser getLoadFilename(Component parent)

Here you can find the source of getLoadFilename(Component parent)

Description

Get a filename to load from.

License

Open Source License

Parameter

Parameter Description
parent a parameter

Declaration

public static File getLoadFilename(Component parent) 

Method Source Code


//package com.java2s;
// Released under Open Works License, http://owl.apotheon.org/

import java.awt.Component;
import java.io.*;

import javax.swing.*;

public class Main {
    private final static JFileChooser _fileChooser = new JFileChooser(System.getProperty("user.dir"));

    /**/* w w w  .  j  ava 2s. c  o  m*/
     * Get a filename to load from.
     * 
     * @param parent
     * @return 
     */
    public static File getLoadFilename(Component parent) {
        _fileChooser.setMultiSelectionEnabled(false);
        _fileChooser.setSelectedFile(new File("x")); // clear the previous selection
        _fileChooser.setSelectedFile(new File(""));
        int option = _fileChooser.showOpenDialog(parent);
        if (option == JFileChooser.APPROVE_OPTION) {
            File filename = _fileChooser.getSelectedFile();
            if (filename.exists()) {
                return filename;
            } else {
                JOptionPane.showMessageDialog(parent,
                        "File " + filename.getName() + " does not exist.  You need to select an existing file.",
                        "Error: No file exists", JOptionPane.ERROR_MESSAGE);
                return getLoadFilename(parent);
            }
        }
        return null;
    }
}

Related

  1. getFileToSave(String description, String extension, Component component)
  2. getFileWithExtension(JFileChooser c)
  3. getFixedFileChooser(File file)
  4. getIcon(File file)
  5. getJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)
  6. getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
  7. getNewFileChooser()
  8. getOpenFile(String defaultPath)
  9. getOpenFile(String name, String currentDirectory)