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 {
    /**
     * Displays a {@link JFileChooser} to select a file.
     *
     * @param title The title of the dialog.
     * @param startDirectory The directory where the dialog is initialed opened.
     * @param fileExtension File extension to filter each content of the opened
     * directories. Example ".xml".
     * @param startFile The preselected file where the dialog is initialed opened.
     * @return The {@link File} object selected, returns null if no file was selected.
     */
    public static File chooseFile(String title, String startDirectory, final String fileExtension,
            String startFile) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle(title);

        if (fileExtension != null && !fileExtension.trim().equals("")) {
            FileFilter filter = new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return pathname.isDirectory() || pathname.getName().endsWith(fileExtension);
                }

                @Override
                public String getDescription() {
                    return "(" + fileExtension + ")";
                }
            };

            chooser.setFileFilter(filter);
        }

        if (startDirectory != null && !startDirectory.trim().equals("")) {
            chooser.setCurrentDirectory(new File(startDirectory));
        }

        if (startFile != null && !startFile.trim().equals("")) {
            chooser.setSelectedFile(new File(startFile));
        }

        int status = chooser.showOpenDialog(null);

        if (status == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        }

        return null;
    }

    /**
     * Displays a {@link JFileChooser} to select a file.
     *
     * @param title The title of the dialog.
     * @param startDirectory The directory where the dialog is initialed opened.
     * @param fileExtension File extension to filter each content of the opened
     * directories. Example ".xml".
     * @return The {@link File} object selected, returns null if no file was
     * selected.
     */
    public static File chooseFile(String title) {
        return chooseFile(title, null, null, null);
    }

    /**
     * Displays a {@link JFileChooser} to select a file.
     *
     * @param title The title of the dialog.
     * @param fileExtension File extension to filter each content of the opened directories. Example ".xml".
     * @return The {@link File} object selected, returns null if no file was selected.
     */
    public static File chooseFile(String title, String fileExtension) {
        return chooseFile(title, null, fileExtension, null);
    }
}