Could anyone please help me :-(
Hi,
here's a question for u. I am using a JFileChooser with an "Open" dialog for choosing a directory by JFileChooser.setFileSelectionMode JFileChooser.DIRECTORIES_ONLY). Now it works fine, except when i double click on a directory, which sets the
directory name in the "File Name" text box, now if I press "Open", it does nothing, coz it cannot find that directory in this directory as it is already in that directory!!
The way to make it work is to clear the "File Name" box and then press "Open", which is very unprofessional and no dumb user will think of doing that.
What feel is that the only way to do this is to be actually able to access/modify the Open dialog box, which as far as I know, there is no way as it is handled by the JVM itself.
Thanks for ur consideration.
here is part of the code: the function, please feel free to use it in ur program and let me know.
protected void setworking()
{
JFileChooser chooser = new JFileChooser(dirs);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle("Set Working Directory");
int returnVal = chooser.showOpenDialog(this);
System.out.println(returnVal);
dirs=(chooser.getSelectedFile()==null?chooser.getCurrentDirectory().getAbsolutePath();+"\\"+chooser.getSelectedFile().getName());
System.out.println("You chose to choose this directory: "+dirs);
}
--
Priya
[1471 byte] By [
priyaC] at [2007-9-26 18:25:37]

I tried this for you, and it works very well for selecting a directory. Or I don't understand the problem...
import javax.swing.*;
class Test
{
public static void main(String[] argument)
{
setworking();
System.exit(0);
}
protected static void setworking()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle("Set Working Directory");
int returnVal = chooser.showOpenDialog(null);
System.out.println(returnVal);
String dirs=chooser.getSelectedFile().getAbsolutePath();
System.out.println("You chose to choose this directory: "+dirs);
}
}
There are several problems with choosing directories from the JFileChooser, (I won't go into them here). I have written the following class to choose directories. I have only tested this some on windows, but it could be a good starting point for others.
[code]
import javax.swing.*;
import java.io.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
public class DirectoryChooser extends JFileChooser {
public DirectoryChooser(File startDir) {
super(startDir);
this.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
public File getSelectedFile() {
Filefile= super.getSelectedFile();
if (file == null) {
return file;
}
String dirName = super.getSelectedFile().getAbsolutePath();
String fileSep = System.getProperty("file.separator", "\\");
if (dirName.endsWith(fileSep + ".")) {
dirName = dirName.substring(0, dirName.length() - 2);
file= new File(dirName);
}
return file;
}
public void setCurrentDirectory(File file) {
// Let the action take place, then override the 'Name' field.
// This is equivalent to the user typing "." in the name field.
super.setCurrentDirectory(file);
FileChooserUI ui = getUI();
if (ui instanceof BasicFileChooserUI) {
((BasicFileChooserUI)ui).setFileName(".");
}
}
}
[\code]