Adit Kirtani
Adit Kirtani

Reputation: 113

Why does JFileChooser work this way?

Hello guys it has been a while. So I am working on this application which allows me to make and save documents blah blah blah. That is not the focus of this question. So in the save feature, I decided to use JFileChooser to allow user to select a location for saving file.

Now my JFileChooser launches properly, here is code for that:

 public void Save () {
        choicer.setCurrentDirectory(new java.io.File("C://"));
        choicer.setDialogTitle("Save Document");
        choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        choicer.setAcceptAllFileFilterUsed(false);
        if (choicer.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
            dir = String.valueOf(choicer.getCurrentDirectory());
        }
        
        File f = new File(dir + "\\hi.txt");
    }

Ignore the hi.txt name. That I will focus on later.

Now there are 2 problems which I have found. First of all, I have to go one folder "deep" to save my file. Explanation:
Let us say I want to save my file in Public in Users, my directory looks like:

C:\Users\Public
Now my code adds \hi.txt in the file parameters, shouldn't this save in the Public folder. If I just save there after opening public, I get an exception:

java.io.FileNotFoundException: C:\Users\Public (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at Spreadr$TableMethods.Save(Spreadr.java:175)
at Spreadr.lambda$1(Spreadr.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

If I however open a folder in public and then click the save, my directory now looks like:

C:\Users\Public\Downloads

It then does not save in the Downloads folder but in the Public User folder.

That is my first problem. My second is actually a mini question type thingy. Is it possible for me to name my file in the Save Menu (JFileChooser menu)?

Upvotes: 0

Views: 103

Answers (1)

nos
nos

Reputation: 229058

Your code calls choicer.getCurrentDirectory() , which will give you the parent directory of whichever directory you're in when selecting a directory in JFileChooser.DIRECTORIES_ONLY mode.

You probably want to get the directory you have selected, which you get by calling

choicer.getSelectedFile()

Upvotes: 2

Related Questions