Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with JFileChooser

  Asked By: Viveka    Date: Feb 21    Category: Java    Views: 1043
  

Is there a way to override the little TextField in JFileChooser showing
my selected file name? The problem is that names of directories show up in this
little text field as well as if they were files, which is not so convenient. If
I then press the load key, it tries to load the non existent file. Please help
me if you have any idea. Linnea Karen HawleyThe Jessica Simpson Fan n:o 1

Share: 

 

1 Answer Found

 
Answer #1    Answered By: May Hansen     Answered 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);

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




Tagged: