[Java] OpenFileDialog anzeigen

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

/**
 * shows a OpenFileDialog
 */
public class OpenFileDialog
{
    private ArrayList<String> _extensiontext = new ArrayList<>();
    private ArrayList<String> _extension = new ArrayList<>();

    /**
     * shows OpenFileDialog
     * @param path initial path, may be null
     * @return selected file
     */
    public File showDialog(String initialpath)
    {
        if ((initialpath == null) || (initialpath.length() == 0))
        {
            initialpath = System.getProperty("user.home");
        }

        final JFileChooser c = new JFileChooser(initialpath);
        
        c.setDialogTitle("Select file");
        c.setDialogType(JFileChooser.OPEN_DIALOG); 
        c.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        // add filters
        for (int i = 0; i < _extensiontext.size(); i++)
        {
            FileNameExtensionFilter ff = new FileNameExtensionFilter(_extensiontext.get(i), _extension.get(i));
            c.setFileFilter(ff);
        }

        // show dialog
        c.setVisible(true);
        
        File f = null;
        
        // if "ok" or "yes" pressed    
        if (c.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            // get selected file
            f = c.getSelectedFile();
        }
        
        // close dialog
        c.setVisible(false);

        return f;
    }

    /**
     * adds FileNameExtensionFilter
     * @param text Filtertext, e.g. "Textfiles (*.txt)"
     * @param ext Filterextension, e.g. "txt"
     */
    public void addExtension(String text, String ext)
    {
        _extensiontext.add(text);
        _extension.add(extension);
    }
}

Aufruf:

OpenFileDialog fd = new OpenFileDialog();
// add an extension
fd.addExtension("Textfiles (*.txt)", "txt");
// open dialog and get selected file
File f = fd.showDialog(null);