2

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;
Christoff Erasmus
  • 925
  • 1
  • 10
  • 26

1 Answers1

1

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

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • 2
    This is dealing with AWT / Swing images, the question is asking about SWT Images which are not the same thing. – greg-449 Jul 28 '14 at 18:11
  • awt is general term and swt is the term given by eclipse to swing + awt. – Deepanshu J bedi Jul 29 '14 at 07:36
  • 1
    Sorry but that is not so. SWT is a completely separate windowing system for Eclipse and does not use anything from Swing or AWT. AWT is not a general term it refers specifically to the java.awt.* classes – greg-449 Jul 29 '14 at 07:38
  • Thanks for guiding me to the Solution, refer to the "Solution Paragraph" for my implementation. – Christoff Erasmus Jul 29 '14 at 07:38
  • they are all used in graphic designing, i can happen that i am wrong check this : http://stackoverflow.com/questions/7358775/java-gui-frameworks-what-to-choose-swing-swt-awt-swingx-jgoodies-javafx – Deepanshu J bedi Jul 29 '14 at 07:42
  • @greg-449 i am not saying that awt + swing = swt but the features are almost the same with some changes...as it was created by IBM – Deepanshu J bedi Jul 29 '14 at 07:45