views:

212

answers:

3

Hi, I have been trying to change the icon in the frame. I have virtually tried everything:

  • The icon is 16x16 which is the right size ....doesn't work

  • I've trying PNG,GIF and JPG formats none of them work.

  • Tried different way of setting the icon....doesn't work.

  • I've tried relative (local paths) e.g. "icon.gif" and absolute paths e.g. "c:\work\java\icon.gif" ...doesn't work

Here is my code and see if you can figure it out Thanks Oli

    import javax.swing.*;
    public class androidDriver 
    {

        public static void main(String[] args) throws IOException 
        {
            JFrame f = new JFrame("Android Data Viewer");
            f.setResizable(false);
            f.setSize(300,300);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            f.setIconImage(new ImageIcon("androidIcon2.gif").getImage());
        }
    }
A: 

I suspect you may have to actually wait for the image to load using a MediaTracker. It's likely that the image is still loading at the point the frame setIconImage references it, so it does nothing.

Software Monkey
IIRC, `ImageIcon` already does that.
Tom Hawtin - tackline
it does do already do that
objects
A: 

Have you tried using Toolkit.getDefaultToolkit().getImage("androidIcon2.gif")

And two other things:

  1. Does the image exist? The code you posted will fail silently.

  2. Is it formatted properly? (though I assume Java could handle it if it wasn't)

jex
yes it does exist, added file.exists() and true was printed out Yes - Java documentation says it need to be 16x16 and either a GIF, JPG or PNG
Oliver
+1  A: 

If you put the image in the same directory as the class file then the following should work for you:

        f.setIconImage(new ImageIcon(androidDriver.class.getResource("androidIcon2.gif")).getImage());

Also would suggest setting the icon image before you make the frame visible

        f.setIconImage(new ImageIcon(androidDriver.class.getResource("androidIcon2.gif")).getImage());
        f.setVisible(true);
objects
The first solution came up with null pointer exceptionException in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at androidDriver.main(androidDriver.java:24)Java Result: 1Tried Second solution but did nothing
Oliver
that suggests it cannot find the image. whats your classpath set to?
objects