Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to load an image from a JAR
How to load an image from a JAR [message #451134] |
Wed, 23 February 2005 23:32  |
Eclipse User |
|
|
|
Hi,
I am simply trying to get an icon in my title bar which I have done
successfully, but it only works with an absolute path. I want to make it
relative to the project, or better yet, relative to the JAR file that it
will be exported to. Is there some way to reference the image by package,
like com.images.myimage.gif?
This is my current code.
Image icon = new Image(display, "F:\\some_image.gif");
parent.setImage(icon);
Thanks for any advice on this
Matt
|
|
| | |
Re: How to load an image from a JAR [message #451194 is a reply to message #451188] |
Thu, 24 February 2005 09:26   |
Eclipse User |
|
|
|
Uhm, I was thinking the new Image would manage to close the stream?
Now looking at the API, on the one hand it is told "Application code
is still responsible for closing the input stream" on the other hand
an example is given with
"This constructor may be used to load a resource as follows:
new Image(device, clazz.getResourceAsStream("file.gif"));"
which does not allow to close the stream since it is created inside
the Image-constructor
Ben
>Don't forget to close the stream.
>
>"Philippe Marschall" <kustos@gmx.net> wrote in message
>news:cvkefp$tjh$1@www.eclipse.org...
>
>
>>Something like this should work
>>
>>InputStream is =
>>
>>
>>
>this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif
>");
>
>
>>Image icon = new Image(display, is);
>>
>>
>>Matt Rossner wrote:
>>
>>
>>>Hi,
>>>
>>>I am simply trying to get an icon in my title bar which I have done
>>>successfully, but it only works with an absolute path. I want to make it
>>>relative to the project, or better yet, relative to the JAR file that it
>>>will be exported to. Is there some way to reference the image by
>>>
>>>
>package,
>
>
>>>like com.images.myimage.gif?
>>>
>>>This is my current code.
>>>
>>> Image icon = new Image(display, "F:\\some_image.gif");
>>> parent.setImage(icon);
>>>
>>>Thanks for any advice on this
>>>
>>>Matt
>>>
>>>
>>>
>>>
>
>
>
>
|
|
|
Re: How to load an image from a JAR [message #451199 is a reply to message #451194] |
Thu, 24 February 2005 09:43   |
Eclipse User |
|
|
|
Originally posted by: broussea.irisa.fr
I have exactly the same issue.
The following SWT code:
Menu menuNew = new Menu(shell , SWT.DROP_DOWN);
MenuItem itemNew = new MenuItem(menuFile , SWT.CASCADE);
itemNew.setText("&New");
itemNew.setMenu(menuNew);
Image img = new Image(display, "images/new.gif");
itemNew.setImage(img);
generates a org.eclipse.swt.SWTException: i/o error
(java.io.FileNotFoundException: images/xml.gif (No such file or directory))
while executing the jar.
I have tried your solution of using an InputStream but it crashes the
application all together.
Also, using the full path to this image generates the
FileNotFoundException again.
Any ideas what can I do?
Cheers.
Benjamin Pasero wrote:
> Uhm, I was thinking the new Image would manage to close the stream?
>
> Now looking at the API, on the one hand it is told "Application code
> is still responsible for closing the input stream" on the other hand
> an example is given with
>
> "This constructor may be used to load a resource as follows:
> new Image(device, clazz.getResourceAsStream("file.gif"));"
>
> which does not allow to close the stream since it is created inside
> the Image-constructor
>
> Ben
>
>
>> Don't forget to close the stream.
>>
>> "Philippe Marschall" <kustos@gmx.net> wrote in message
>> news:cvkefp$tjh$1@www.eclipse.org...
>>
>>
>>> Something like this should work
>>>
>>> InputStream is =
>>>
>>>
>>
>> this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif
>>
>> ");
>>
>>
>>> Image icon = new Image(display, is);
>>>
>>>
>>> Matt Rossner wrote:
>>>
>>>
>>>> Hi,
>>>>
>>>> I am simply trying to get an icon in my title bar which I have done
>>>> successfully, but it only works with an absolute path. I want to
>>>> make it
>>>> relative to the project, or better yet, relative to the JAR file
>>>> that it
>>>> will be exported to. Is there some way to reference the image by
>>>>
>>
>> package,
>>
>>
>>>> like com.images.myimage.gif?
>>>>
>>>> This is my current code.
>>>>
>>>> Image icon = new Image(display, "F:\\some_image.gif");
>>>> parent.setImage(icon);
>>>>
>>>> Thanks for any advice on this
>>>>
>>>> Matt
>>>>
>>>>
>>>>
>>
>>
>>
>>
>>
|
|
| | |
Re: How to load an image from a JAR [message #451319 is a reply to message #451194] |
Fri, 25 February 2005 09:56  |
Eclipse User |
|
|
|
https://bugs.eclipse.org/bugs/show_bug.cgi?id=86631
"Benjamin Pasero" <bpasero@rssowl.org> wrote in message
news:cvko7e$ere$1@www.eclipse.org...
> Uhm, I was thinking the new Image would manage to close the stream?
>
> Now looking at the API, on the one hand it is told "Application code
> is still responsible for closing the input stream" on the other hand
> an example is given with
>
> "This constructor may be used to load a resource as follows:
> new Image(device, clazz.getResourceAsStream("file.gif"));"
>
> which does not allow to close the stream since it is created inside
> the Image-constructor
>
> Ben
>
>
> >Don't forget to close the stream.
> >
> >"Philippe Marschall" <kustos@gmx.net> wrote in message
> >news:cvkefp$tjh$1@www.eclipse.org...
> >
> >
> >>Something like this should work
> >>
> >>InputStream is =
> >>
> >>
> >>
>
>this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gi
f
> >");
> >
> >
> >>Image icon = new Image(display, is);
> >>
> >>
> >>Matt Rossner wrote:
> >>
> >>
> >>>Hi,
> >>>
> >>>I am simply trying to get an icon in my title bar which I have done
> >>>successfully, but it only works with an absolute path. I want to make
it
> >>>relative to the project, or better yet, relative to the JAR file that
it
> >>>will be exported to. Is there some way to reference the image by
> >>>
> >>>
> >package,
> >
> >
> >>>like com.images.myimage.gif?
> >>>
> >>>This is my current code.
> >>>
> >>> Image icon = new Image(display, "F:\\some_image.gif");
> >>> parent.setImage(icon);
> >>>
> >>>Thanks for any advice on this
> >>>
> >>>Matt
> >>>
> >>>
> >>>
> >>>
> >
> >
> >
> >
|
|
|
Goto Forum:
Current Time: Wed Jul 16 08:12:18 EDT 2025
Powered by FUDForum. Page generated in 0.07434 seconds
|