choose File using JFileChooser - Java Swing

Java examples for Swing:JFileChooser

Description

choose File using JFileChooser

Demo Code


//package com.java2s;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static File chooseFile() {
        JFileChooser chooser = new JFileChooser();
        File f = null;//  w  w  w . ja  v  a 2s . c  o m
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "Audio Files", "wav", "mp3", "flac");
        //chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(new JFrame(""));
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            f = chooser.getSelectedFile();
            System.out.println("You chose to open this file: "
                    + f.getName());
        }
        return f;
    }
}

Related Tutorials