【问题标题】:Is it possible to use a FileDialog from inside an Intellij Plugin?是否可以从 Intellij 插件内部使用 FileDialog?
【发布时间】:2019-09-06 09:24:34
【问题描述】:

和这个answer类似,在使用文件对话框的时候,需要传入一个JFrame。

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

我尝试过像这样转到根窗格,但它返回的是一个 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());

【问题讨论】:

    标签: java intellij-idea intellij-plugin


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我在我的问题中添加了一个工作代码示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多