1

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

tenorsax
  • 21,123
  • 9
  • 60
  • 107
hectichavana
  • 1,436
  • 13
  • 41
  • 71
  • 1
    is icon.getImageLoadStatus() == MediaTracker.COMPLETE – Mike K. Sep 25 '11 at 18:35
  • your question might be a duplicate of: http://stackoverflow.com/questions/7194734/setting-application-icon-in-swing – Jessica Brown Sep 25 '11 at 18:49
  • not really, because in this case I have nothing to do with JFrame – hectichavana Sep 25 '11 at 19:01
  • I tried that, I do not also, I don't know where is problem, but sometimes look here one from authors of this Framework, maybe give us answer :-) – mKorbel Sep 25 '11 at 21:53
  • this.getFrame() is returning either an instance of Frame or JFrame. You described your question as being swing in both the title and tags, it is not an unreasonable assumption that your frame is a JFrame rather than an AWT Frame. – Jessica Brown Sep 27 '11 at 20:24

2 Answers2

2

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.

Jessica Brown
  • 8,222
  • 7
  • 46
  • 82
  • everything seems correct including the resource path, because on another box I set an Image on the same folder (resource) and I use the same writing – hectichavana Sep 25 '11 at 19:02
  • are you on a case-sensitive OS? I do notice you have capital letters in your image filename. – Jessica Brown Sep 25 '11 at 19:11
  • JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.setIconImage(new ImageIcon("/resources/BilKa_Icon_32.png").getImage()); I have put those inside the class, yet a new window popped up but still the icon of the new window didn|t change as well. – hectichavana Sep 25 '11 at 19:22
  • Did you test the demo project from the new window tutorial? Does it work for you? It works for me on my machine. If it works, that narrows down the problem to something in your code, vs. window decorations don't work on your OS/java version/etc. The source code is linked from that tutorial, just need to create the package (and optionally add a jpg) and compile/run: http://download.oracle.com/javase/tutorial/displayCode.html?code=http://download.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemo2Project/src/components/FrameDemo2.java – Jessica Brown Sep 27 '11 at 20:26
2

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());
Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • I got this error: The Constructor MediaTracker(DesktopApplication1View) is undefined. And I need one more parameter for mt.addImage – hectichavana Sep 25 '11 at 19:30