Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Viveka Fischer   on Feb 21 In Java Category.

  
Question Answered By: May Hansen   on Feb 21

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);

Share: 

 
 
Didn't find what you were looking for? Find more on Problem with JFileChooser Or get search suggestion and latest updates.


Tagged: