open File Chooser - Java Swing

Java examples for Swing:JFileChooser

Description

open File Chooser

Demo Code


import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

public class Main{
    private static final FileFilter DEFAULT_FILE_FILTER = new GraphFileFilter();
    public static File openFileChooser(JFrame ownerFrame) {
        JFileChooser fc = new JFileChooser(new File(
                AppConstants.DEFAULT_FOLDER_PATH));
        fc.setAcceptAllFileFilterUsed(false);
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setFileFilter(DEFAULT_FILE_FILTER);

        switch (fc.showOpenDialog(ownerFrame)) {
        case JFileChooser.APPROVE_OPTION:
            return fc.getSelectedFile();
        default://from   w  ww .  j a  v a  2 s  .com
            return null;
        }
    }
}

Related Tutorials