Question

Question:

I would like to know how to create an org.eclipse.swt.graphics.Image object from a .gif in an external .jar file.

Back Ground:

I am creating a TreeViewer based on TreeViewerArticle, and would like to reference a picture in the jt400.jar file for one of my tree elements.

Code Snippet:

class ViewLabelProvider extends LabelProvider {

   public Image getImage(Object obj) {

      <<<<NEED CODE to create org.eclipse.swt.graphics.Image >>>>

   }
} 

Solution:

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/com/ibm/as400/access/AS40016.gif");
ImageData id = new ImageData(stream);
Image image = new Image(null, id);
return image;
Was it helpful?

Solution

Try this:- Toolkit.getDefaultToolkit().getImage(getClass().getResource("File dir"));

if getResource doesnt work try this.

public BufferedImage loadImage(String fileName){

    BufferedImage buff = null;
    try {
        buff = ImageIO.read(getClass().getResourceAsStream(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return buff;

} 

try something like:

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");

In your JAR file, you might have a directory structure of:

jt400.jar
- com (class files in here)
- images
----image.gif

try these methods

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top