Make file dialog JFileChooser - Java Swing

Java examples for Swing:JOptionPane

Description

Make file dialog JFileChooser

Demo Code


//package com.java2s;

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

public class Main {
    /**//from   w  w w  .  ja v a2 s . co m
     * Make file dialog
     *
     * @param thisTitle
     *            dialog title
     * @param thisExtension
     *            file extension
     * @param thisDescription
     *            description of file type
     * @return JFileChooser
     */
    static public JFileChooser makeFileDialog(final String thisTitle,
            final String thisExtension, final String thisDescription) {
        final FileFilter thisFileFilter = new FileFilter() {
            private final String[] theExtensions = { thisExtension };

            @Override
            public boolean accept(final File thisFile) {
                for (final String thisExtension : this.theExtensions)
                    if (thisFile.getName().toLowerCase()
                            .endsWith(thisExtension))
                        return true;
                return thisFile.isDirectory();
            }

            @Override
            public String getDescription() {
                return thisDescription;
            }
        };
        JFileChooser thisFileChooser;
        thisFileChooser = new JFileChooser();
        thisFileChooser.setDialogTitle(thisTitle);
        thisFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        thisFileChooser.setFileFilter(thisFileFilter);
        thisFileChooser.setCurrentDirectory(new File(System
                .getProperty("user.dir")));
        return thisFileChooser;
    }
}

Related Tutorials