Android JFileChooser Show selectFileForRead(Component parent)

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

Description

Select a file for read opt.

Declaration

public static File selectFileForRead(Component parent) 

Method Source Code

//package com.java2s;
import java.awt.Component;

import java.io.File;

import javax.swing.JFileChooser;

public class Main {

    public static File selectFileForRead(Component parent) {
        // Create a file chooser
        final JFileChooser fc = new JFileChooser("");

        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

        // In response to a button click:
        int returnVal = fc.showOpenDialog(parent);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return fc.getSelectedFile();
        }//from w  ww . j a va  2 s  .  c o m

        if (returnVal == JFileChooser.CANCEL_OPTION) {
            return null;
        }

        throw new RuntimeException("file exception when select file.");
    }
}