dialog Open File - Java Swing

Java examples for Swing:JOptionPane

Description

dialog Open File

Demo Code


//package com.java2s;
import java.io.File;
import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {

    public static File dialogOpenFile(final String[] fileTypes,
            String title, String defaultDir) {
        JFileChooser jfileChooser = new JFileChooser();
        jfileChooser.setDialogTitle(title);
        jfileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        if (defaultDir != null && defaultDir.length() > 0) {
            jfileChooser.setCurrentDirectory(new File(defaultDir));
        }//from  w w w  .jav  a 2 s.  c o m
        jfileChooser.setFileFilter(new FileFilter() {

            @Override
            public String getDescription() {
                // TODO Auto-generated method stub
                String desc = "";
                for (int i = 0; i < fileTypes.length; i++) {
                    desc += fileTypes[i] + ";";
                }
                return desc;
            }

            @Override
            public boolean accept(File file) {
                // TODO Auto-generated method stub
                if (file.isDirectory() == true) {
                    return true;
                }
                String fileName = file.getName().toLowerCase();
                if (fileTypes[0].equals(".*") == true) {
                    return true;
                }
                for (int i = 0; i < fileTypes.length; i++) {
                    if (fileName.endsWith(fileTypes[i]) == true) {
                        return true;
                    }
                }
                return false;
            }
        });
        if (jfileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            return jfileChooser.getSelectedFile();
        }
        return null;
    }
}

Related Tutorials