FileChooser fails to open a directory path
Hi
can someone tell me how can i know if FileChooser fails to open a directory path.
I am trying to open a Shared folder on a network pc using JFileChooser.
But if the PC is offline the FileChooser shows the contents of the My Documents folder. Is there any way to catch this so that I can display a suitable error to the user.
[352 byte] By [
Sumitk31a] at [2007-11-27 5:39:15]

# 1
The showOpenDialog() method shows the open dialog, and pauses execution until it is closed. It returns a value depending on the state of the dialog when it is closed. For example, the constant JFileChooser.APPROVE_OPTION means that a file was selected, and you may retrieve it with the getSelectedFile() method. Basically, it's like this, assuming jfc is the File Chooser object:
int dialogstate = jfc.showOpenDialog(null);
if (dialogstate == jfc.APPROVE_OPTION) {
dosomething();
}
Here are the values that can be returned:
JFileChooser.APPROVE_OPTION
JFileChooser.CANCEL_OPTION
JFileChooser.ERROR_OPTION
So JFileChooser.ERROR_OPTION is the return value you're looking for.
# 2
Actually what is happening is that since the pc is not accessible i get back to the default foldfer that is My documents folder.i want to catch this action . I am not looking for the user cancellation event .
# 3
Fine.boolean readable=jfc.getSelectedFile().canRead();boolean writable=jfc.getSelectedFile().canWrite();If the file can't be read, readable will be false, so you will know that you can't access it.
# 4
I suppose you are using constructor JFileChooser(String currentDirectoryPath) to instantiate, or you explicitely set somehow the current directory to the shared folder's path...
Have you tried this:
boolean b = myFileChooser.getCurrentDirectory().exists();
// or
boolean b = myFileChooser.getCurrentDirectory().isTraversable(myFileChooser.getCurrentDirectory());
// then
if(b)
open_the_dialog_then_proceed();
else
error_handling();
// before calling the showOpenDialog() method
# 5
jfc=new JFileChooser("//"+serverip+"/Content/");
jfc.setAcceptAllFileFilterUsed(false);
jfc.addChoosableFileFilter(new ImageFilter());
jfc.setMultiSelectionEnabled(true);
boolean readable = jfc.getSelectedFile().canRead();
In the above code the user inputs the server ip and then i open a FileChooser to read the Content folder on that particular server.
If the server is offline then the default folder i.e. My Documents is opened.
I want to catch this error scenario.
boolean readable = jfc.getSelectedFile().canRead();
this can be used only after the user has either pressed the open button or the cancel button.
# 6
try this:
import java.io.*;
// ..........
File myDir = new File("//"+serverip+"/Content/");
if(myDir.exists())
{
jfc=new JFileChooser(myDir);
jfc.setAcceptAllFileFilterUsed(false);
// ......... your code contilnues here
}
else
{
error_handling();
}
# 7
thanks a lot . It works now