Java examples for Swing:JFileChooser
choose File using JFileChooser
//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; } }