1

Is there any way that when the JFileChooser loads, it only display folder having name "Hello" only.

Here is my code: It displays all folders and also file having extension .py and .java. I want to add that folder name restriction to it.

FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Select Source Code To Analyze", "java","py");
            jfc.setFileFilter(filter);
            //jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    JButton btnNewButton = new JButton("Select Erroneous File"); //SELECT File Button.
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             if (jfc.showOpenDialog(contentPane) !=
                        JFileChooser.APPROVE_OPTION)
                            return;
                    File f = jfc.getSelectedFile();

Current Program Output:

enter image description here

I want the output to be somewhat like this:Only Display Folder Having name "Hello" and rest of files only. enter image description here

Ms_Joe
  • 160
  • 2
  • 7
  • 16
  • 1
    See [How do I restrict JFileChooser to a directory?](http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory) – Reimeus Mar 11 '13 at 02:30
  • 1
    See also these [custom file explorers](http://stackoverflow.com/q/15149565/230513). – trashgod Mar 11 '13 at 02:35
  • Check out [JFileChooser#addChoosableFileFilter](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#addChoosableFileFilter(javax.swing.filechooser.FileFilter)), there is also an example at the top of the page – MadProgrammer Mar 11 '13 at 03:00
  • See: [Single Root File Chooser](http://tips4java.wordpress.com/2009/01/28/single-root-file-chooser/). – camickr Mar 11 '13 at 03:02
  • @MadProgrammer - adding a filter only affects selectable items, not displayed items – radai Mar 11 '13 at 05:03
  • @radai Ah, that depends on the Look and Feel, but under Windows (if I recall properly) it will filter the displayed list – MadProgrammer Mar 11 '13 at 06:19

1 Answers1

2

Use File filter like this.

javax.swing.JFileChooser jfc = new javax.swing.JFileChooser();
jfc.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
jfc.setFileFilter(new javax.swing.filechooser.FileFilter(){
    @Override
    public boolean accept(java.io.File file){
        //return (file.isDirectory() && file.getName().equals("Hello")) || !file.isDirectory(); 
        // Get only hello folder and .py files
        //return (file.isDirectory() && file.getName().equals("Hello")) || (!file.isDirectory() && file.getName().toLowerCase().endsWith(".py"));
        // Get only hello folder and .java files if Hello folder is opened else .py files
        return (file.isDirectory() && file.getName().equals("Hello")) || 
        (!file.isDirectory() && file.getParentFile().getName().equals("Hello") && 
        file.getName().toLowerCase().endsWith(".java")) || 
        (!file.isDirectory() && file.getName().toLowerCase().endsWith(".py"));
    }
    @Override
    public String getDescription() {
        return "Hello Folder and Other Files";
    }
});
Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
  • Thanks It works..Is there any way to add a condition when I click the Hello folder, inside it it list only files having ".java" extension and normally when JFileChooser loads, it list only Hello folder and files having ".py" extension. I dont know where to put this condition. – Ms_Joe Mar 11 '13 at 08:11
  • Just extend the condition in `accept` method itself. Updated answer. – Ravindra Gullapalli Mar 11 '13 at 08:25
  • Yup it works..but when I choose Hello Folder, Inside that folder Where to put the condition to Display only .java files. Really appreciate that. – Ms_Joe Mar 11 '13 at 08:29
  • Already written a condition for that in the program. `return (file.isDirectory() && file.getName().equals("Hello")) || (!file.isDirectory() && file.getName().toLowerCase().endsWith(".java"));` will retrieve only java files what ever folder you visit. Even inside `Hello` folder. – Ravindra Gullapalli Mar 11 '13 at 08:34
  • No thats not what I mean. Normally when the Jfilechooser loads, it will display only Folder Hello and files with .py Extension.This part is ok and working perfectly. Now when I choose the Hello folder to view its file contents, i want it to display files with .java extension only. Hope You understand the question. – Ms_Joe Mar 11 '13 at 08:37
  • Updated my answer. This will retrieve Hello folder and .py files and if you open Hello folder, it will show .java files. – Ravindra Gullapalli Mar 11 '13 at 09:36