public static String selectFile ( boolean exitOnCancel ) { String fileName = null ; JFileChooser fc = new JFileChooser ( ) ; int returnVal = fc . showOpenDialog ( null ) ; if ( returnVal == JFileChooser . APPROVE_OPTION ) { fileName = fc . getSelectedFile ( ) . getAbsolutePath ( ) ; } else if ( exitOnCancel ) { System . exit ( 0 ) ; } return fileName ; }
Opens up a dialog box asking the user to select a file . If the user cancels it either returns null or quits the program .