Search code examples
javaintellij-ideaintellij-plugin

Is it possible to use a FileDialog from inside an Intellij Plugin?


Similar to this answer, when using a file dialog, you need to pass in a JFrame.

I have a button on a JPanel inside a ConfigurableUI Menu, and would like to open a file dialog when clicking it. This file dialog needs to load in a file or directory outside of the project and plugins resource folders... e.g /root/path/to/file.xyz

I've tried to go to the root pane like so, but it returns a Window object instead.

public class MyConfigurableUI implements ConfigurableUi<MyPlugin> {

    JPanel panel;

    ...
    public void pressButtonAction(){
        //This doesnt work
        //JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(panel);


        FileDialog fd = new FileDialog(panel, "Choose a file", FileDialog.LOAD);
        //How do I get the JFrame to pass in?
    }



    @NotNull
    @Override
    public JComponent getComponent() {
        return panel;
    }

}

I have looked in the code samples and have not found an example using a file dialog.

**Edit:**

Using the answer below I was able to open a file with the following code:

FileChooserDescriptor fcDesc = new FileChooserDescriptor(true,false,false,false,false,false);
FileChooserDialog fcDial = FileChooserFactory.getInstance().createFileChooser(fcDesc, null, null);
VirtualFile[] files = fcDial.choose(null);
//do something with the path
doSomething(files[0].getPath());

Solution

  • Please use builtin com.intellij.openapi.fileChooser.FileChooserFactory