Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Component;

import java.io.File;

import java.util.regex.Pattern;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {

    public static String showOpenFile(String currentDirectoryPath, Component parent, final String filterRegex,
            final String filterDescription) {
        JFileChooser fileChooser = new JFileChooser(currentDirectoryPath);
        fileChooser.addChoosableFileFilter(new FileFilter() {
            private Pattern regexPattern = Pattern.compile(filterRegex);

            public boolean accept(File f) {
                if (f.isDirectory())
                    return true;
                return regexPattern.matcher(f.getName()).matches();
            }

            public String getDescription() {
                return filterDescription;
            }

        });
        fileChooser.showOpenDialog(parent);

        File choosedFile = fileChooser.getSelectedFile();
        if (choosedFile == null)
            return null;
        return choosedFile.getAbsolutePath();
    }
}