java - 是否可以从 Intellij 插件内部使用 FileDialog?

标签 java intellij-idea intellij-plugin

与此类似answer ,使用文件对话框时,需要传入一个JFrame。

我在 ConfigurableUI 菜单内的 JPanel 上有一个按钮,并且希望在单击它时打开一个文件对话框。此文件对话框需要加载到项目和插件资源文件夹外部的文件或目录中...例如/root/path/to/file.xyz

我尝试像这样转到根 Pane ,但它返回一个 Window 对象。

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;
    }

}

我查看了 code samples并且还没有找到使用文件对话框的示例。

**编辑:**

使用下面的答案,我可以使用以下代码打开一个文件:

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());

最佳答案

请使用内置com.intellij.openapi.fileChooser.FileChooserFactory

关于java - 是否可以从 Intellij 插件内部使用 FileDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57741262/

相关文章:

java - LinkedHashMap 中的 LinkedHashSet

java - 使用 Spring boot 运行集成测试 (NoSuchBeanDefinitionException)

java - 如何在 spring security 中模拟 wicket setResponsePage()

java - 下拉帮助java

java - 部分项目Intellij中StringBuilder改为java.lang.StringBuilder

java - IntelliJ插件开发: how to generate a new source file

java - Intellij插件开发-访问最近激活的编辑器的内部堆栈

java - 获取 IntelliJ 中打开的项目中所有 PsiFiles/VirtualFiles 的列表

java - Nexus 5 上的 Android 手电筒

Java JDBC Mysql 连接只在 try catch 内部有效