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

6

1 Answer 1

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";
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

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.
Just extend the condition in accept method itself. Updated answer.
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.