Problem displaying Icons in JTree

Hi!

To load Icons for each instance of different TreeNodes in the JTree I used following method:

activatedIcon =

new ImageIcon(Toolkit.getDefaultToolkit().getImage(path));

where the path is set as "org/app/images/UnInit.gif"

The separate folder "org/app/images" holds all the icons of the application!

The method works fine when I run the application from an IDE (ex. SAP Netweaver). The JTree is created with Icons!

But when I create a executable JAR file, the JTree is created without icons!

I tried another way with the java.net.URL.

try{

java.net.URL imgURL = getClass().getResource(path);//ImageStates.class.getClassLoader().getResource(path);

if(imgURL !=null){

Image img = ImageIO.read(imgURL);

activatedIcon =new ImageIcon(img);

}

}catch(Exception e){}

First disadvantage for my application in this case is the huge memory consumption! ( I have to create around 5000 nodes at one strech, and that is must)

Secondally, when I run the application with this method, it populates the JTree slowly and shows up nodes with icons. Fine!

But when I create a executable JAR file, the URL at runtime is computed as NULL, and hence rest of the block fails!

Could anyone please help me as of how I could read Images (as Icons for my Nodes) when the application has to be executed as standalone JAR application!

[1821 byte] By [PRXa] at [2007-10-2 4:27:56]
# 1
Have you triednew ImageIcon(url);rather than using ImageIO?Hope this helps.
KPSeala at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi Seal,

Yes I tried to use the URL in creating ImageIcon

System.out.println("Path used was: " + path);

java.net.URL imgURL = getClass().getResource(path);//ImageStates.class.getClassLoader().getResource(path);

System.out.println("URL used was :" + imgURL);

activatedIcon = new ImageIcon(imgURL);

When I packaged this in JAR and executed,

I got following output:

Path used was : "/org/app/images/UnInit.gif"

URL used was : null

(and all the exception stack messages)

Any ideas?

Thanks in anticipation

PRXa at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 3
If your URL is null then ImageIO won't load the image either!Are your images actually in your JAR file?Are they in /org/app/images/UnInit.gif?Is the case of your filenames correct?
KPSeala at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi Seal,

Offcourse each resource of the application including the images are packed in the JAR and the image "UnInit.gif" is exactly in the package directory "/org/app/images"!

Do you mean I should create a new JAR only for resources and use this jar in the Classpath of "java -jar" command?

PRXa at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 5
There's no need to create a new JAR just for your resources (although you can if you want).Is the output of the System.out you showed earlier correct? The quotes around the path shouldn't be there...
KPSeala at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hi,The quotes had been added just to make the path clearer. It wasn't in the output screen.I am just looking for a simle running example, with which I could proceed further! That would be very helpful!
PRXa at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 7

Here's something that works for me.

Icons are in /basic/icons directory of my JAR.

protected void initComponentDefaults(UIDefaults table) {

// ...

table.put("OrderBlotter.buyIcon", loadIcon("/basic/icons/OrderBlotter.buyIcon.gif"));

table.put("OrderBlotter.sellIcon", loadIcon("/basic/icons/OrderBlotter.sellIcon.gif"));

}

protected IconUIResource loadIcon(String name) {

URL url = getClass().getResource(name);

if(url == null) {

System.err.println("Could not load icon: " + name);

return null;

}

return new IconUIResource(new ImageIcon(url));

}

Hope this helps.

KPSeala at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...
# 8
I also recommend not using java -jar to begin with. Try java -cp MyJar.jar com.me.MyClassIf that works then at least you know it's not a trivial issue in your code!
KPSeala at 2007-7-15 23:56:50 > top of Java-index,Desktop,Core GUI APIs...