Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why won't the frame Icon change?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why won't the frame Icon change?

    I'm a newb to GUI and I'm trying to wrap my head around it. I don't quite understand why I am unable to change the frame icon in my GUI application. I chose a picture from "my Pictures" and entered the file's name as a string. However the icon never changes. Please explain why it is true and what can be done to solve this issue.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package practice;
    import java.awt.*;
    import java.awt.image.ImageObserver;
    import java.awt.image.ImageProducer;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    /**
     *
     * 
     */
    public class Practice extends JFrame {
        public Practice(){
     
          super("MyEmptyFrame");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Toolkit tk = Toolkit.getDefaultToolkit();
          Image frameIcon = tk.getImage("002.jpg");
          setIconImage(frameIcon);
          setExtendedState(MAXIMIZED_BOTH);
     
    }
        public static void main(String[] args) {
    	Practice frame = new Practice();
    	    frame.setVisible(true);
    	}
     
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why won't the frame Icon change?

    It's curious that you showed some of the required imports but some others that are required are not included and still others are unnecessary for the code you posted. Are you getting errors?

    Problems that involve loading resources stored somewhere in another development environment are hard to troubleshoot. If you're getting errors, posting those would be helpful. You could check to see if the frameIcon is null after you assign it. You could add your own try/catch to exit if the .jpg file isn't being found, the frameIcon is null, whatever you'd like. Knowing that would be helpful.

    I loaded your code and modified it to point to a .png file on my system (I didn't have a .jpg ready to go), and it worked fine. The only change I made was the file path and name. I know where my resources are and how to load them. You may be going through the struggles of learning how to do that.

    Give us more info if you can. Good luck!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why won't the frame Icon change?

    Quote Originally Posted by GregBrannon View Post
    It's curious that you showed some of the required imports but some others that are required are not included and still others are unnecessary for the code you posted. Are you getting errors?

    Problems that involve loading resources stored somewhere in another development environment are hard to troubleshoot. If you're getting errors, posting those would be helpful. You could check to see if the frameIcon is null after you assign it. You could add your own try/catch to exit if the .jpg file isn't being found, the frameIcon is null, whatever you'd like. Knowing that would be helpful.

    I loaded your code and modified it to point to a .png file on my system (I didn't have a .jpg ready to go), and it worked fine. The only change I made was the file path and name. I know where my resources are and how to load them. You may be going through the struggles of learning how to do that.

    Give us more info if you can. Good luck!
    I didn't get any errors but I used a print statement and called the fileexists() method and it returned false but I'm sure the file exists. I've been able to see the image outside the IDE.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why won't the frame Icon change?

    Then the path to the file is incorrect. When I was learning how relative and absolute file addressing worked in my IDE, I moved the file around to the possible locations in the project's workspace until the program was able to find it with the specified file address. It was trial and error - mostly error - but I had a better understanding of how file addressing worked when I was done. From that, I developed my own "standard" for where program resources should be located and how they should be addressed.

Similar Threads

  1. Instance Variable Won't Change!!
    By wawawarrior in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2013, 11:07 PM
  2. [SOLVED] While loop won't allow variable to change
    By Thor in forum Loops & Control Statements
    Replies: 13
    Last Post: October 9th, 2012, 08:35 PM
  3. JToggleButton's icon won't show up when pressed!
    By andreiutz10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2011, 05:03 AM
  4. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Java Swing
    Replies: 2
    Last Post: July 27th, 2010, 03:28 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM