Question

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();

Im wondering why the image icon doesnt show up on the top left of the app window. It`s still the Java cup of coffee logo instead.

Is there anything wrong?

Thank you

Was it helpful?

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.

OTHER TIPS

Mike K is right, ImageIcons can be loaded dynamically, and images can have a zero size when they are first initialised. Also note that in Unix and in a JAR, the names are case sensitive.

try this:

 try{
   ImageIcon icon = new ImageIcon("resources/BilKa_Icon_32.png");
   MediaTracker mt=new MediaTracker(this);
   mt.addImage(icon.getImage(),0);
   mt.waitForAll();
   this.getFrame().setIconImage(icon.getImage());
 }catch(InterruptedException excp){}

--

OK apologies I have edited the addImage - it takes an extra parameter ID which can be any number. As to your error "no such constructor", it is telling you that you need to pass a Component to the constructor. Your app window is a component, so you should pass that here as a parameter. I used this because most people put this code inside the class that extends Frame, Window or JFrame. So use

MediaTracker mt=new MediaTracker(this.getFrame());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top