Question

So I'm experimenting with the tray menu and I have the line.

final TrayIcon trayIcon = new TrayIcon(createImage("duke.jpg", "tray icon"));

The method createImage is

protected static Image createImage(String path, String description) {
    URL imageURL = TrayIconDemo.class.getResource(path);

    if (imageURL == null) {
        System.err.println("Resource not found: " + path);
        return null;
    } else {
        return (new ImageIcon(imageURL, description)).getImage();
    }
}

when I run the program I get the following error

Resource not found: duke.jpg
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: creating TrayIcon with null Image
    at java.awt.TrayIcon.<init>(Unknown Source)
    at misc.TrayIconDemo.createAndShowGUI(TrayIconDemo.java:76)
    at misc.TrayIconDemo.access$0(TrayIconDemo.java:68)
    at misc.TrayIconDemo$1.run(TrayIconDemo.java:63)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Here is a picture of my folder showing where the image is.

http://i.stack.imgur.com/80tFY.png

I would just like to state that I found this http://www.oracle.com/technetwork/articles/javase/systemtray-139788.html and it works regardless if there is an image or if there isn't. Also the image had to be placed in the source folder and NOT the folder the .java was in for it to show so I'm assuming it has to be there for the original code that my questions is in regards to. So if anyone needs help with the System Tray just check the link.

Était-ce utile?

La solution

If you change this method like this, it works.

protected static Image createImage() {
            String path="bulb.gif";
            String description="";
            String imageURL = path;
            if (imageURL == null) {
                System.err.println("Resource not found: " + path);
                return null;
            } else {
                return (new ImageIcon(imageURL, description)).getImage();
            }
        }

Autres conseils

The easiest way you can do is System.out.println(new File(".").getAbsolutePath()) to find out what is considered as a root path and then adjust the actual path to the file.

I use this because there is a difference between webapp (Servlet-based) and Java SE app.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top