user3279373
user3279373

Reputation: 11

JFileChooser selected direcory

I have next code:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btnNajitPDFCache) {
        JFileChooser chooser;
           String choosertitle = "Select directory.";
        chooser = new JFileChooser(); 
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setApproveButtonText("OK");
        //
        // disable the "All files" option.
        //
        chooser.setAcceptAllFileFilterUsed(false);
        //    
        if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
            textFieldPDFCache.setText(chooser.getCurrentDirectory()+"");
        }
    }
}

That's Ok. I choose c:\test folder in opened chooser form and next I click on button OK.

But chooser.getCurrentDirectory() return only c:\ . Why? What is wrong?

Upvotes: 1

Views: 105

Answers (3)

JavaTechnical
JavaTechnical

Reputation: 9357

getCurrentDirectory() returns the current directory that is opened in the JFileChooser. When you are selecting C:\test you opened C:\ directory, so you are getting C:\ on getCurrentDirectory()

The getSelectedFile() returns the file that is selected (in your case the file is a directory). So you if you want the directory that is selected by the user use getSelectedFile()

Upvotes: 3

Sachin Khendake
Sachin Khendake

Reputation: 87

You Can Set Canonical path as File Canonicalpath = new File(new File("C:/").getCanonicalPath());

Upvotes: 0

ahmedalkaff
ahmedalkaff

Reputation: 313

You should use chooser.getSelectedFile() instead.

Upvotes: 2

Related Questions