Results 1 to 10 of 10
  1. #1
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default change icon of frame?

    Hi,

    I was working on an application in java swing and the problem which i encountered was that when i tryed to use the method i.e setImageIcon(icon) to change the icon of the frame,it never worked! i haved posted the code below,kindly please suggest me on where i have gone wrong.

    class me extends JFrame
    {

    .........
    ImageIcon x=new ImageIcon("c:\\pics\cups.java");
    setImageIcon(x);
    ..........

    }


    Kind Regards,
    Manish87
    Last edited by Manish87; 01-30-2011 at 03:52 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Make sure the path to the image is a valid path (which it is not based upon the code you posted - which won't even compile)

  3. #3
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi,

    Thanks for the reply! Even thought i set a proper directory path i.e c:\\pics\\cups.png the code wont work!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    It might help if you don't invent methods that don't exist.

    db

  5. #5
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi Darryl,

    Sorry for the inconvince,i always misspell the method setIconImage() with setImageIcon().neways,i have corrected out the mistakes pointed by you,still the code doesnt seem to compile.after little bit of "googling" i have found out that we should use the method that is getImage() after u create an object of setIconImage,whereas if you want to place an icon in menuitems, setIconImage() is itself sufficient! i would be really thankfull to you if you could tell me on why should we use getImage() specifically for frame!

    /* corrected code */

    {

    .........
    ImageIcon x=new ImageIcon("c:\\pics\cups.java");
    setIconImage(x);
    ..........

    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What the is "setIconImage" method (I believe that this is what Darryl was referring to when he advised you not to make up methods), and where have you found a method such as this? Is this a method that you have elsewhere in the class? And what is the precise error message that you are seeing? You need to give us a bit more information if we are to help you.
    Last edited by Fubarable; 01-30-2011 at 04:49 AM.

  7. #7
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi,

    setIconImage method can be called by creating an instance of JFrame i.e obj.setIconImage(icon); neways i have fixed the problem,we can set an icon to frame with the help of getImage() method,here's the code

    class Example extends JFrame
    {
    public Example()
    {
    initUI();
    }

    public void initUI()
    {
    setTitle("hello");
    setLocation(500,200);
    setSize(200,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    ImageIcon icon=new ImageIcon("c:\\pics\\me.png"); //SET PATH OF THE IMAGE
    setIconImage(icon.getImage());

    }
    }

    Thanx a lot for the support Fubarable and darryl and doWhile :-) :-)

    Kind Regards,
    Manish87

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Constructing an ImageIcon only to retrieve its Image is unnecessary. There are at least two ways to load an Image from a file or resource, using ImageIO or Toolkit. Read the APis for those classes to find the methods that are useful.

    db

  9. #9
    raghuana is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Solution of your problem is : use c:\\pics\\me.jpg Hope it will be help you

    Try this

    setIconImage((new ImageIcon("c:\\pics\\me.jpg")).getImage());

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by raghuana View Post
    Solution of your problem is : use c:\\pics\\me.jpg Hope it will be help you

    Try this

    setIconImage((new ImageIcon("c:\\pics\\me.jpg")).getImage());
    A very bad answer to a thread that is old and has already been solved. Locking.

Similar Threads

  1. Change the icon of file in java
    By smn in forum SWT / JFace
    Replies: 18
    Last Post: 10-06-2010, 04:55 PM
  2. Swing: Change the icon on a JComponent
    By xytho33 in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2010, 06:53 AM
  3. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM
  4. Icon change and lib folder problem
    By LeonLanford in forum New To Java
    Replies: 10
    Last Post: 10-21-2009, 04:13 PM
  5. Custom icon for Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •