How do I add an icon as a classpath resource to an SWT window created with WindowBuilder?
Asked Answered
R

6

16

I'm trying to add an external icon from an *.ico file to a window that I'm creating using the WindowBuilder design window. I can select the shell, which brings up an "image" properties field. alt text That brings up the image chooser dialog box: alt text

How do I make my icon show up in this menu as a classpath resource? The image works if an absolute path is given, but I don't want to use that option in my application.

Thanks!

Romanticism answered 23/12, 2010 at 19:49 Comment(0)
R
29

To easily add an icon to my classpath, I found my desired icon, right clicked it, selected "copy", then went to one of the packages of my project in Eclipse, right clicked, and selected "paste". The next time I brought up the image chooser dialog box, my local package had the icon listed as an available classpath resource, and I chose it. image chooser

I was able to export the project to a runnable JAR, and the icon still worked.

Romanticism answered 3/1, 2011 at 16:38 Comment(3)
I was able to get the image files to successfully show up as a classpath resource, but if I selected them for my JLabel icon, it wouldn't appear. If I selected the image as an absolute path, it appears. I've confirmed the files exist in their package. What could be the issue?Dinin
@Dinin It worked fine with me, and the image could be set for my JLabel iconBeach
It worked. After doing this, I changed the source removing the path to the image and deleted the copy. It is now using the original.Nonperishable
J
6

The solution I find to be working is to create a jar containing your images and add it to your class path. Then you will be able to choose them from the dialog in your second screen shot.

I remember this used to work with directories that are in your build path. Now it seems to be forced to be in a jar package.

Jiggered answered 30/12, 2010 at 19:14 Comment(2)
How do you create a jar file containing only one image?Romanticism
a jar file is simply a zip archive. use any archive utility (I prefer 7-zip) to create a zip file and change its extension to jar.Scarificator
H
3

To add any kind of supported image to your project, just right click on 'src' folder of your project and New... Package... and under Name give, for example, 'resources'. After that you only need to copy your images there. When you export the project to a runable JAR, all resources go together and Runs fine.

Halfsole answered 26/8, 2011 at 23:10 Comment(0)
W
2

I don't know how to do this in WindowBuilder, but you can specify an Image resource while building the Shell via setImage() or setImages(). I suggest using the latter, because it provides the platform with various resolution icons, including the window's control box, the Windows taskbar, and alt+tab list.

Take a look at this snippet.

To load it from a resource:

final Image small = new Image(shell.getDisplay(),
        "resources/images/icon_16.png");
final Image large = new Image(shell.getDisplay(),
        "resources/images/icon_32.png");
final Image[] images = new Image[] { small, large };
shell.setImages(images);

In this example, I have a subfolder "resources", containing "images", then two PNGs. Specifying a resource JAR should work in a similar way, although I haven't tried it.

Winters answered 30/12, 2010 at 17:33 Comment(0)
B
1

In my case, WindowBuilder recognized the *.ico format but didn't replace the default Java icon with my custom icon. It was only when I converted the *.ico to *.png (through this handy online tool) that WindowBuilder finally changed the default Java icon to my custom "icon", even though it's really a PNG. I expected WindowBuilder to be able to recognize the ICO format.

Backwater answered 29/3, 2013 at 2:51 Comment(0)
A
0

In Eclipse Juno 4.2. the Image chooser often doesn't show the resource folder (e.g. from a Maven-structured project: src/main/resources. Presumably that is a bug.

If you remove and then add the resource folder explicitly with the include option in the Java Build Path Window (Source Tab), it will pop up. Even after removing the "include" option and setting it back to "All", it will still show.

Of course, you may remove and add directly from the context menu, when right-clicking the src/main/resources folder.

Apelles answered 4/1, 2013 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.