Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with JFileChooser FileSystemView and Type Column in Details View

829136Aug 21 2011 — edited Aug 21 2011
I am looking for suggestions on dealing with the JFileChooser and a FileSystemView under Windows XP Java SE JDK 1.6.0_24

I'm using the JFileChooser to allow the user to browse files, but need to restrict access to where the user can browse and also provide the equivalent of "symbolic links" so that he may jump to alternate locations. These restrictions occur only at certain phases of the application when the user has invoked a specific function for which this view of the file system is appropriate.

My initial solution was to extend FileSystemView and provide my own version of the getFiles method which provides a list of only those directories that are appropriate for the application at that time. The "symbolic links" are simply Java File objects that I insert into the file list before returning it to the user (they are always VALID file objects pointing to existing directories... I checked). Everything worked great and I was even able to use custom icons and "virtual file names" to let the user browse the file system.

The problem arises when the user invokes the Details view. When he does, the "Type" column for the inserted files is blank and the date string appears in the type column. While not fatal, this is unsightly.

Here's the weird part...

I have tried overriding the getSystemTypeDescription in the FileSystemView. I have tried installing a custom FileView overriding both the getTypeDescription and getDescription method. I have tried overriding the JFileChoosers getDescription and getTypeDescription methods. I have put break points in all these methods and even in some of the Java swing classes (debugging using netbeans) such as JFileChooser and the parent FileSystemView and FileView classes.

It appears that none of these methods even get invoked. The break points are not reached. Other methods, getIcon, getName, etc. do get invoked. But none of the methods listed above even get called. And while I cannot entirely rule out "operator error" in this, I've spent a lot of time on it and it seems less likely.

Does anyone have ideas about what my be going on?

Thanks in advance for your help.

Gary

P.S. I apologize that I cannot post a code sample at this time, but it's a little involved and it would take a fair amount of carving down to get it here on the forum. I can do so with some effort if nobody has any insights right off the bat.

Edited by: G. W. Lucas on Aug 21, 2011 8:11 AM

The following code snippet was added a couple of hours after the initial post
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;

public class TestChooser extends JPanel {
    
    class TestView extends FileSystemView {

        File normalFolder;
        File specialFolder;
        FileSystemView nativeFSV;
        
        TestView(File normalFolder, File specialFolder) {
            this.normalFolder = normalFolder;
            this.specialFolder = specialFolder;
            this.nativeFSV = FileSystemView.getFileSystemView();
        }
        
        @Override
        public File createNewFolder(File containingDir) throws IOException {
            return nativeFSV.createNewFolder(containingDir);
        }
        
        @Override
        public File[] getFiles(File directory, boolean useFileHiding) {
            // in actual implementation, there is a bit more selection logic
            boolean addSpecial = !directory.equals(specialFolder);
            File[] f = nativeFSV.getFiles(directory, useFileHiding);
            if (!addSpecial) {
                return f;
            }
            File[] g = new File[f.length + 1];
            g[0] = specialFolder;
            System.arraycopy(f, 0, g, 1, f.length);
            return g;
        }
        
        @Override
        public String getSystemTypeDescription(File file){
            // this method is never invoked         
            if(file.isDirectory())
                return "File Folder";
            return "Some File";
        }
    }

    public TestChooser() {
        super(new BorderLayout());
        //    SomeComponent someComponent = new SomeComponent();
        //    add(someComponent, BorderLayout.CENTER);
    }
    
    private File createFolder(File parent, String folderName) {
        File file = new File(parent, folderName);
        if (!file.exists()) {
            file.mkdir();
        }
        return file;
    }    
    
    private TestView createTestView() {
        File rootFolder = new File("c:/TempUser/FileSystemViewTest");
        if (!rootFolder.exists()) {
            rootFolder.mkdirs();
        }
        File normalFolder = createFolder(rootFolder, "normal");
        createFolder(normalFolder, "alpha");
        createFolder(normalFolder, "beta");
        File specialFolder = createFolder(rootFolder, "special");
        return new TestView(normalFolder, specialFolder);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI(String[] args) {
        //Create and set up the window.
        JFrame frame = new JFrame("Empty Panel Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        TestChooser testPanel = new TestChooser();
        testPanel.setPreferredSize(new Dimension(500, 500));
        //Add content to the window.
        frame.add(testPanel);
        
        
        TestView testView = testPanel.createTestView();
        final JFileChooser chooser = new JFileChooser(testView) {
            @Override
            public String getTypeDescription(File file) {
                // this method is never invoked         
                if (file.isDirectory()) {
                    return "File Folder";
                }
                return "Some File";
            }
        };
        chooser.setCurrentDirectory(testView.normalFolder);
        final JButton button = new JButton("Perform Open Test");
        button.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                chooser.showOpenDialog(button);
            }
        });
        
        testPanel.add(button);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        
        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            System.err.println("exception: " + ex);
        } catch (InstantiationException ex) {
            System.err.println("exception: " + ex);
        } catch (IllegalAccessException ex) {
            System.err.println("exception: " + ex);
        } catch (UnsupportedLookAndFeelException ex) {
            System.err.println("exception: " + ex);
        }
        
        final String[] finalArguments = args;

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI(finalArguments);
            }
        });
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 18 2011
Added on Aug 21 2011
5 comments
571 views