【发布时间】: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