与此类似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/