System Tray image won't display
hello all....i am trying to create a system tray icon for a windown xp machine....i have the following code
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
Image image = Toolkit.getDefaultToolkit().getImage("lightening.png");
trayIcon = new TrayIcon(image, "Tray Demo", popup);
the program runs fine, but the image isn't displayed...only a transparent box.......i am not sure where to place the image....should i place it in the same folder as the .class file, or the .java file, or what exactly....and what should the path of the getDefaultToolkit().getImage() method........at the moment, it is located in both directories, and still no image....the image its self is a 16 by 16 png file
any help is apreciated..
thanks alot
sam
[899 byte] By [
semsem22a] at [2007-10-3 2:58:32]

/**
* Returns an image which gets pixel data from the specified file,
* whose format can be either GIF, JPEG or PNG.
* The underlying toolkit attempts to resolve multiple requests
* with the same filename to the same returned Image.
* Since the mechanism required to facilitate this sharing of
* Image objects may continue to hold onto images that are no
* longer of use for an indefinite period of time, developers
* are encouraged to implement their own caching of images by
* using the createImage variant wherever available.
* @paramfilenamethe name of a file containing pixel data
* in a recognized file format.
* @returnan image which gets its pixel data from
* the specified file.
* @see #createImage(java.lang.String)
*/
Toolkit.getDefaultToolkit().getImage(path);
If you use only the file name "lightening.png",then you must put the file to the root folder of your classes.
I.E:
If all your .class files are in the folder
C:\bin
, then put your image into that folder as well.
If you are using packages
E.G:
C:\bin\mypackage
, which contain your .class files
E.G:
C:\bin\mypackage\MyApplet.class
, then put the image file into
C:\bin
But I suggest you to keep your images in a separate folder
E.G:
C:\bin\img
In that case you should refer to it as
Toolkit.getDefaultToolkit().getImage("img/lightening.png");
cause it uses the path relative to the root folder.
I hope it helped.
Use ImageIcon to load the image. It blocks until the thing is actually loaded and available. I think Toolkit doesn't.
i am using NetBeans IDE 5.0.........for each project, NetBeans creates a build folder, a dist folder for the distribution of the package files....the dist folder has the jar file in it....and i just tried to put the image in the same folder as the jar file, and it is still not working....THERE IS NO bin folder.....there is a src folder, a nbproject folder, which i am not sure of the its contents
so where should i put the image with respect to the the build folder....inside the build folder, there is a folder called classes, i put the image there and it is not working, i put the image inside the folder that has the .class files, and still not working...please help
thanks
sam
can you give me an example code of how to use ImageIcon
/**
* Returns an ImageIcon, or null if the path was invalid.
* */
private static ImageIcon getImageIcon(String path, String altDescription, StartApplet baseApplet)
{
final String methodName = "getImageIcon";
//Image img = Toolkit.getDefaultToolkit().getImage(path);
URL
imgURL=null;
if(path.indexOf("p://")==-1){
imgURL = baseApplet.getResource(path);
}else{
try{
imgURL = new URL(path);
}catch(MalformedURLException exception){
Log.logError(className, methodName ,exception);
}
}
if (imgURL != null) {
//Log.println(className+"."+methodName+" Found file: " + path);
return new ImageIcon(imgURL, altDescription);
} else {
//Log.println(className+"."+methodName+" Couldn't find file. path: " + path+", imgURL: " + imgURL);
return null;
}
}
to call this method :
ImageIconicon = getImageIcon(filename, altDescription, baseApplet);
// baseApplet refers to the active Applet instance
My StartApplet class has a method:
/**
* Returns a URL relative to the StartApplet's or its extender's location, or null if the path was invalid.
* */
public URL getResource(String path) {
final String methodName = "getResource";
URL uRL = StartApplet.class.getResource(path);
Log.println(className+"."+methodName+" uRL "+uRL);
return uRL;
}
Sorry:public class StartApplet extends javax.swing.JAppletAnd block the Log with "//", please.
I'm not Using NetBeans, but you shouldn't put your images to the same folder with .jar.
I suppose, it has a folder called "public_html", where you could create a subfolder for your images.
I suppose, you could open MyComputer or WindowsExplorer to find the folder, where NetBean creates the package folders with compiled .class files to determine the relative path to your images.
I'm not sure, if I was understandable enough.
thank you very much for your help, i got it to work by playing around with the path for a while, it is using a relative path to main project folder, so i made an images folder, and corrected to the path to direct to that, and now it is workingthank you very muchsam