AGirlFromMontreal (1) [Avatar] Offline
#1
I'm writing an Eclipse plugin (following an example I found on the net) and try to load small icons in a table. In my LabelProvider class I have this:

static {
String iconPath = "icons/";
imageRegistry.put(
CHECKED_IMAGE,
ImageDescriptor.createFromFile(
EbbTableViewer.class,
iconPath + CHECKED_IMAGE + ".gif"));

... more the same...

}

and here's the method that gets the image:

private Image getImage(boolean isSelected) {
String key = isSelected ? CHECKED_IMAGE : UNCHECKED_IMAGE;
return imageRegistry.get(key);
}

But no image is drawn. I got this error:

The image could not be loaded: FileImageDescriptor(location=class tableviewerexample.TableViewerExample, name=icons/unchecked.gif)
org.eclipse.jface.resource.DeviceResourceException : Unable to create resource FileImageDescriptor(location=class tableviewerexample.TableViewerExample, name=icons/unchecked.gif)
at org.eclipse.jface.resource.ImageDescriptor.createR esource(ImageDescriptor.java:167)
at org.eclipse.jface.resource.DeviceResourceManager.a llocate(DeviceResourceManager.java:56)
at org.eclipse.jface.resource.AbstractResourceManager .create(AbstractResourceManager.java:8smilie
at org.eclipse.jface.resource.ResourceManager.createI mageWithDefault(ResourceManager.java:192)
at org.eclipse.jface.resource.ImageRegistry.get(Image Registry.java:211)
at tableviewerexample.ExampleLabelProvider.getImage(E xampleLabelProvider.java:56)
at tableviewerexample.ExampleLabelProvider.getColumnI mage(ExampleLabelProvider.java:8smilie
at org.eclipse.jface.viewers.TableColumnViewerLabelPr ovider.update(TableColumnViewerLabelProvider.java: 71)
at org.eclipse.jface.viewers.ViewerColumn.refresh(Vie werColumn.java:135)
at org.eclipse.jface.viewers.AbstractTableViewer.doUp dateItem(AbstractTableViewer.java:386)
at org.eclipse.jface.viewers.StructuredViewer$UpdateI temSafeRunnable.run(StructuredViewer.java:466)
at org.eclipse.jface.util.SafeRunnable$3.run(SafeRunn able.java:154)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnab le.java:199)
at org.eclipse.jface.viewers.StructuredViewer.updateI tem(StructuredViewer.java:2026)
at org.eclipse.jface.viewers.AbstractTableViewer.crea teItem(AbstractTableViewer.java:264)
at org.eclipse.jface.viewers.AbstractTableViewer.inte rnalRefreshAll(AbstractTableViewer.java:741)
at org.eclipse.jface.viewers.AbstractTableViewer.inte rnalRefresh(AbstractTableViewer.java:633)
at org.eclipse.jface.viewers.AbstractTableViewer.inte rnalRefresh(AbstractTableViewer.java:620)
at org.eclipse.jface.viewers.AbstractTableViewer$2.ru n(AbstractTableViewer.java:576)
at org.eclipse.jface.viewers.StructuredViewer.preserv ingSelection(StructuredViewer.java:136smilie
at org.eclipse.jface.viewers.StructuredViewer.preserv ingSelection(StructuredViewer.java:1330)
at org.eclipse.jface.viewers.AbstractTableViewer.inpu tChanged(AbstractTableViewer.java:574)
at org.eclipse.jface.viewers.ContentViewer.setInput(C ontentViewer.java:251)
at org.eclipse.jface.viewers.StructuredViewer.setInpu t(StructuredViewer.java:1606)
at tableviewerexample.TableViewerExample.addChildCont rols(TableViewerExample.java:155)
at tableviewerexample.TableViewerExample.<init>(Table ViewerExample.java:46)
at tableviewerexample.TableViewerExample.main(TableVi ewerExample.java:85)

can anyone help me?
carolinenew (1) [Avatar] Offline
#2
Re: Can not load image in SWT/Eclipse
hello, i am also writing an image plugin and now i have write some codes on image loading. i'd like to share with you,
namespace RE__Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string fileName = "c:/Sample.png";

REImage reImage = REFile.OpenImageFile(fileName);

REFile.SaveImageFile(reImage, "c:/reimage.png", new PNGEncoder());
}
}
}

Message was edited by:
carolinenew
454805 (1) [Avatar] Offline
#3
First Thing you need to do is, get the URL of the image where it is stored.

URL url=getClass().getResource("/icons/full/ovr16/add_ov.gif");

Then you have created the image for the URL with the help of 'ImageDescriptor'.

ImageDescriptor addIcon = ImageDescriptor.createFromURL(url);

now you have registered this image with 'ImageRegistry' which set your image file as key Value-pair.

imageRegistry.put("default", addIcon);


now your image is already ready for use.

TableItem tableItem = new TableItem(table, SWT.NONE);
Image image = imageRegistry.get("default");
tableItem.setImage(1, image);



here is the full code:

Table table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
URL url=getClass().getResource("/icons/full/ovr16/add_ov.gif");
ImageRegistry imageRegistry = new ImageRegistry();
ImageDescriptor addIcon = ImageDescriptor.createFromURL(url);
imageRegistry.put("default", addIcon);
Image image = imageRegistry.get("default");
tableItem.setImage(0, image);