Nothing coming up in FileChooser dialog

Hi

I want to allow user to select file(s) from the project. I tried the below ways.

1.)
VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), myProject, <myProject>.getBaseDir());

2.)
FileChooserDescriptor descriptorFactory = new FileChooserDescriptor(true, false, false, false, false, false);

                        descriptorFactory.setShowFileSystemRoots(true);
                        descriptorFactory.setHideIgnored(false);
                        VirtualFile files = FileChooser.chooseFile(myProject, descriptorFactory);
                        //VirtualFile files = FileChooser.chooseFile(createSnippetData.getProject(), descriptorFactory, null);
                        //VirtualFile files = FileChooser.chooseFile(createSnippetData.getProject(), descriptorFactory, null, null);

3.)
VirtualFile files = FileChooser.chooseFile(myProject, FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), null);

The dialog comes up, but no files are show (in all the cases). My Project has a few files and classes, attached is the screen shot of the same.
I m working with Intellij 12.0.1

Could you please point out if there anything more to be done.



Attachment(s):
image.png
9 comments
Comment actions Permalink

This problem happens when the code that invokes the file chooser is holding a write action. As a result, the background thread filling the file chooser with contents waits forever to take a read action, and the contents is never displayed. Please make sure that your code showing the file chooser is not inside a runWriteAction() block.

0
Comment actions Permalink

The code is inside addActionListener of a JButton.

0
Comment actions Permalink

Then the code which is showing the dialog containing the button is inside a runWriteAction() block. You can put a breakpoint at the line that shows the file chooser and check the call stack to find where exactly the runWriteAction() is.

0
Comment actions Permalink

yes.

I have public void executeWriteAction(Editor editor, DataContext dataContext) method, from which i call another method abc. from abc the dialog is invoked.
But i have to go through this path only. With this constraint, how can i show files?

0
Comment actions Permalink

You can't. You need to change the logic of your code so that you show the dialog outside of a write action, and then, after the dialog is closed, take the write action and perform the modifications based on the data selected in the dialog.

0
Comment actions Permalink

Is there any other dialog that i can use?

0
Comment actions Permalink

You can use any dialog you want. The standard dialog used in IntelliJ IDEA for choosing files is the one you're trying to use, and it's the one you should use in order to be consistent with other pieces of the IntelliJ IDEA UI.

0
Comment actions Permalink

Can i enclose the call to open the dialog from ApplicationManager.getApplication().runReadAction() or ApplicationManager.getApplication().invokeLater().

somthing like this

System.out.println(ApplicationManager.getApplication().isReadAccessAllowed()); ///this returns true;
                        ApplicationManager.getApplication().invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), <myProject>, <myProject>.getBaseDir());
                                if (file != null) {
                                    System.out.println(file.getName());
                                }
                            }
                        });


I tried this, but it dint work..Please suggest some way out

0
Comment actions Permalink

invokeLater() will work, yes. Adding a runReadAction() will not make any difference, because the event dispatch thread always has read access anyway.

0

Please sign in to leave a comment.