Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Creates a new JFileChooser and returns the selected path's location.
     * @param title
     * @param openTo
     * @param chooseDirectory
     * @param filter
     * @return
     */
    public static String getFilePath(String title, String openTo, boolean chooseDirectory, FileFilter filter) {
        String location = null;
        JFileChooser chooser = new JFileChooser(title);
        chooser.setCurrentDirectory(new File(openTo));
        chooser.setFileFilter(filter);
        chooser.setDialogTitle("Open Cache");
        if (chooseDirectory) {
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        } else {
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        }
        chooser.setAcceptAllFileFilterUsed(false);
        if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
            location = chooser.getSelectedFile().getAbsolutePath();
        }
        return location;
    }
}