The following are some java source code snippets for getting and saving a file:
public file getInputFile(){
File inputFile = null;
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
inputFile = fileChooser.getSelectedFile();
}
return inputFile;
}
public File getOutputFile(){
File outputFile = null;
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
outputFile = fileChooser.getSelectedFile();
}
return outputFile;
}
JFileChooser also has a method that controls what you see in the dialog:
setFileSelectionMode
public void setFileSelectionMode(int mode)
Sets the JFileChooser to allow the user to just select files, just select
directories, or select both files and directories. The default is
JFilesChooser.FILES_ONLY.
Parameters:
mode - the type of files to be displayed:
JFileChooser.FILES_ONLY
JFileChooser.DIRECTORIES_ONLY
JFileChooser.FILES_AND_DIRECTORIES
snippets:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
or
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
or
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);