Most read:
Popular archives:
Java Q&A Forums - Let the great migration begin
We're pleased to announce the first phase of the integration of the Java Q&A Forums with our community platform, JavaWorld's
Daily Brew. Whether you're one of our longtime forum users or a brand newbie, we hope you'll visit the Java Q&A Forums in their new home alongside JW Blogs.
| Enterprise AJAX - Transcend the Hype |
| Oracle Compatibility Developer's Guide |
I've created a frame that extends from the Frame class. I'd like to change the top-left window icon to a user-defined icon. Is it possible?
This question comes up often. Luckily, there is a simple way to set a frame's icon. If you look at the java.awt.Frame API, you'll notice a method:
public void setIconImage(Image image);
To set the frame's icon, you simply pass the setIconImage() method an image. (By the way, Swing's JFrame works the same way, since it directly extends java.awt.Frame.)
You have a few options for obtaining your icon's image, but it's easiest to obtain it from the java.awt.Toolkit instance:
Toolkit.getDefaultToolkit().getImage("icon_name");
The Toolkit's getImage() method returns an image.
Calling the setIconImage on a standard java.awt.Frame instance works as I outlined above. It should work in your derived class as well. For a user-defined icon you'll need to
pass in an icon name at runtime. Three quick ways to do this are:
-Dicon=icon_name.gif
When you specify a parameter on the command line you can easily retrieve the value by calling:
System.getProperty("icon")
Properties work through string/value pairs. The getProperty() method will return the string value for the key specified on the command line. Again, this is not the best approach, since
it will annoy your users.
your_program.cfg param1=value1 param2=value2 icon=icon_name.gif ... paramN=valueN
To use the configuration file, create a java.util.Property instance, open the file with a java.io.FileInputStream, and load it into the Property by calling the Property's load() method. To obtain the icon name, call the Property's getProperty() method and pass it the string icon.
However you decide to get the icon name, once you have it, just create the image and pass it onto the frame, as outlined above.
java.awt.Frame