javaswingimageicon

ImageIcon doesn't show up on top left app window


I have this code:

 public DesktopApplication1View(SingleFrameApplication app)
    {
        super(app);
        pbu.registriere(this);

        ImageIcon icon = new ImageIcon("resources/BilKa_Icon_32.png");
        this.getFrame().setIconImage(icon.getImage());
      
        initComponents();

I'm wondering why the image icon doesn't show up on the top left of the app window. It's still the Java cup of coffee logo instead.

What might be wrong?


Solution

  • One likely possibility is your resource path could be incorrect. Depending on what your file hierarchy, and whether your class files are in a jar, etc. you might need a "/" at the beginning of the path before the res to make the path absolute instead of relative. Tutorial: http://download.oracle.com/javase/1.5.0/docs/guide/lang/resources.html

    If you are fairly confident you are reading the image correctly (a good test would be to make a dummy component inside your window and see whether you can load the image into that), you should look into following through the Frame/Top Level Window Tutorial, particularly the parts about window decorations. In particular, one thing you may not be doing (I can't tell from your snippet) is that it appears you might need to set JFrame.setDefaultLookAndFeelDecorated(true); before the frame is created...which you would not be able to do using this.getFrame(), but need to do somewhere earlier in your initialization code.