Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.swing.*;

import java.awt.*;
import java.io.File;

public class Main {
    /**
     * opens a FileChooser to select a folder
     *
     * @param c parent
     * @return null if nothing is selected, otherwise the absolutePath
     */
    public static String openFolder(Component c, File currrentDirectory) {
        File file = openJFileChooser(c, currrentDirectory, JFileChooser.DIRECTORIES_ONLY, null);
        if (file != null) {
            return file.getAbsolutePath();
        }
        return null;
    }

    /**
     * opens a file using a JFileChooser
     *
     * @param c          parent
     * @param mode       selection Mode {@link JFileChooser}
     * @param buttonText buttonText, uses "Open" when null ({@link JFileChooser})
     * @return File
     */
    @SuppressWarnings({ "SameParameterValue", "WeakerAccess" })
    public static File openJFileChooser(Component c, File currentDirectory, int mode, String buttonText) {
        JFileChooser fc = new JFileChooser(currentDirectory);
        fc.setFileSelectionMode(mode);
        int result;
        if (buttonText != null) {
            result = fc.showDialog(c, buttonText);
        } else {
            result = fc.showOpenDialog(c);
        }
        if (result == JFileChooser.APPROVE_OPTION) {
            return fc.getSelectedFile();
        }
        return null;
    }
}