import java.io.File; import java.io.IOException; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; public class SelectaFile extends JFrame { String curDir ,textDir,system_type; String[] the_selected=new String[2]; JFileChooser chooser ; public static void main(String[] args){ System.out.println( new SelectaFile("Windows").get_File()[0]); } public SelectaFile(String system_type) { this.system_type=system_type; if (system_type.equalsIgnoreCase("Windows")) { } chooser= new JFileChooser(); } public String[] get_File(){ if (system_type.equalsIgnoreCase("Windows")) { chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.setMultiSelectionEnabled(false); chooser.setAcceptAllFileFilterUsed(false); chooser.addChoosableFileFilter(new FileNameExtensionFilter( "Java Files","java")); int option = chooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File sf = chooser.getSelectedFile(); the_selected[0]=sf.getName(); try { the_selected[1]=sf.getCanonicalPath(); } catch (IOException evt) { System.out.println("Exception: "+evt); } } if (option == JFileChooser.CANCEL_OPTION) { the_selected[0]=the_selected[1]="nothing selected"; } } return the_selected; } }