在eclipse插件中查找图标

时间:2013-05-06 14:32:11

标签: url jar eclipse-plugin uri bundle

我想在 Eclipse插件中获取图标 os位置,我使用此代码:

URL fileURLIconChoose = bundle.getEntry("pluginIcons/iconConfirm.png");
String pathIconChoose = null;
try {
  pathIconChoose = FileLocator.resolve(fileURLIconChoose).toURI().getPath();
} catch (URISyntaxException e1) {
  e1.printStackTrace();
} catch (IOException e1) {
  e1.printStackTrace();
}
JButton confirm = new JButton(new ImageIcon(pathIconChoose));   

当我在 eclipse环境中运行代码时,它运行正常,但是当我 export 我的产品并在Eclipse中安装它时,它会失败。

如何解决图标加载问题?有没有其他方法可以访问插件中的图标?

1 个答案:

答案 0 :(得分:0)

您需要使用FileLocator.toFileURL(URL url) API。

javadoc可以看出,当您需要访问该文件时,它会将文件内容提取到磁盘。

重新编写代码

URL fileURLIconChoose = bundle.getEntry("pluginIcons/iconConfirm.png");
String pathIconChoose = null;
try {
  pathIconChoose = FileLocator.toFileURL(fileURLIconChoose).toURI().getPath();
} catch (URISyntaxException e1) {
  e1.printStackTrace();
} catch (IOException e1) {
  e1.printStackTrace();
}
JButton confirm = new JButton(new ImageIcon(pathIconChoose));