4 Replies Latest reply: May 31, 2012 2:10 PM by DarrylBurke RSS

    Working of Jfilechooser

    937323
      How to give security check or access permissions to files and folders with selection of files or folder from the local system on click of the browse button.



      Example:

           final JFileChooser filechooser = new JFileChooser();
                // Changes done for defect 185 - All files not showing in browse location screen.
                filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                filechooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
                
                PDFFileFilter = new ExtensionFileFilter("PDF Files", new String[] { "PDF"});
                HTMLFileFilter = new ExtensionFileFilter("HTML Files", new String[] {"HTML","HTM"});
                EXCELFileFilter = new ExtensionFileFilter("EXCEL Files", new String[] {"XLS"});

                filechooser.setFileFilter(EXCELFileFilter);
                filechooser.setFileFilter(HTMLFileFilter);
                filechooser.setFileFilter(PDFFileFilter);
                
                btnBrowse.addActionListener(new ActionListener() {
                     public void actionPerformed(ActionEvent arg0) {
                          
                          int status = filechooser.showSaveDialog(null);
                          if (status == JFileChooser.APPROVE_OPTION) {
                     File selectedFile = filechooser.getSelectedFile();
                     txtbxFileChooser.requestFocus();
                     try{
                                    AccessController.checkPermission(new FilePermission(selectedFile.toString(), "write,read"));
      //                              AccessController.checkPermission(new Permission(selectedfile.toString(), "read"));
                                    
                                    txtbxFileChooser.setText(selectedFile.toString());
                                    }
                                    catch(SecurityException e1)
                                    {
                                         JOptionPane.showMessageDialog(null, "Access denied");
                                    }
                          
                     } else if (status == JFileChooser.CANCEL_OPTION) {
                     }
                     }
                });
        • 1. Re: Working of Jfilechooser
          Sreram
          May be you can try this.

          try{
          AccessController.checkPermission(new FilePermission(selectedFile.toString(), "write,read"));
          txtbxFileChooser.setText(selectedFile.toString());
          }
          catch(AccessControlException e1)
          {
          if(e1.getPermission().getActions().contains("write") && e1.getPermission().getActions().contains("read")){
          txtbxFileChooser.setText(selectedFile.toString());
          }else{
          JOptionPane.showMessageDialog(null, "Access denied");
          }
          }

          Note: write permission need not necessarily mean read permission, especially in linux based systems.
          • 2. Re: Working of Jfilechooser
            EJP
            All that access controller stuff only checks Java permissions granted by the security manager, based on the .policy file. If there isn't a security manager it doesn't even do that.

            The permissions granted by the operating system are visible via File.isReadable() and friends.
            • 3. Re: Working of Jfilechooser
              DarrylBurke
              The permissions granted by the operating system are visible via File.isReadable() and friends.
              And you can write a <tt>javax.swing.filechooser.FileFilter </tt>implementation that prevents unacceptable files from even being listed in the <tt>JFileChooser</tt>.

              db
              • 4. Re: Working of Jfilechooser
                DarrylBurke
                >
                Don't double post the same or a follow up question, especially without having the courtesy to reply to responses on this thread.

                Your new thread How does the JFileChooser checks file permissions? has been locked.

                Also, announcements at the top of the forum listing are meant to be read. Clearly, you didn't bother.

                db