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 20 of 20

Thread: JFrame - ImageIcon not displaying

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JFrame - ImageIcon not displaying

    Hey, I was hoping someone could point out why my image may not be showing. Note I am using eclipse.

    Window.java:

    import javax.swing.*; 
     
    public class Window 
    {
    	private JFrame gameWindow; 
     
    	public Window() 
    	{
    		gameWindow = new JFrame("Tamagotchi!");
    		gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		gameWindow.setBounds(0,0,800,600);
     
    		SplashScreen splashScreen = new SplashScreen(); 
    		gameWindow.add(splashScreen.getContent());
    		gameWindow.setVisible(true);
    	}
     
    	public static void main(String[] args)
    	{
    		new Window();
    	}
     
    }

    SplashScreen.java:

    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class SplashScreen
    {
    	private JPanel splashPanel;
    	private JLabel background;
    	private ImageIcon backgroundImage;
     
    	public SplashScreen()
    	{
    		splashPanel = new JPanel();
    		backgroundImage = new ImageIcon("splash_screen.png");
    		background = new JLabel(backgroundImage);
    		splashPanel.add(background);
    		splashPanel.setBounds(0,0,800,600);
    	}
     
    	public JPanel getContent() {
    		return splashPanel;
    	}
    }

    I have used file.exists() and this returns true, and file.length() which returns the correct file size.
    System.out.println(backgroundImage) returns "splash.png" so thats ok.

    Why then is my image not showing?

    Thanks for reading.
    Last edited by mohagan9; April 15th, 2014 at 07:50 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: JFrame - ImageIcon not displaying

    Does the layout you're using respect the bounds you've set? Normally you either use a layout *or* setBounds, not both.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by KevinWorkman View Post
    Does the layout you're using respect the bounds you've set? Normally you either use a layout *or* setBounds, not both.
    Using setBounds and the panel is within bounds of the JFrame. Doesn't help, just a blank JFrame.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: JFrame - ImageIcon not displaying

    Your code works fine for me. What does your directory structure look like?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by KevinWorkman View Post
    Your code works fine for me. What does your directory structure look like?
    Thanks for testing it! Here's my structure:

    Project Folder:

    >src >>default package
    >jre
    >splash_screen.png

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: JFrame - ImageIcon not displaying

    To see what is happening add a println() to the getContent() method that prints the bounds for the background object.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by Norm View Post
    To see what is happening add a println() to the getContent() method that prints the bounds for the background object.
    Here's the output:

    javax.swing.JPanel[,0,0,800x600,invalid,layout=java.awt.FlowLayout,al ignmentX=0.0,alignmentY=0.0,border=,flags=9,maximu mSize=,minimumSize=,preferredSize=]

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: JFrame - ImageIcon not displaying

    Sorry, I'd just moved some folders and that was a ghost.

    Try minimizing the frame and restoring it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by mohagan9 View Post
    Thanks for testing it! Here's my structure:

    Project Folder:

    >src >>default package
    >jre
    >splash_screen.png
    Yep, that's pretty much what mine looks like. Try printing this to see if Java is looking in the right place:

    System.out.println(new File("splash_screen.png").getAbsolutePath());

    --- Update ---

    Quote Originally Posted by Norm View Post
    Sorry, I'd just moved some folders and that was a ghost.

    Try minimizing the frame and restoring it.
    Oh yeah, I just realized OP is adding the JPanel to the JFrame after the JFrame has already been displayed. OP, is there a reason you're doing this?

    You have to revalidate and repaint your window for components that have been added after the JFrame is already visible.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by KevinWorkman View Post
    Yep, that's pretty much what mine looks like. Try printing this to see if Java is looking in the right place:

    System.out.println(new File("splash_screen.png").getAbsolutePath());

    --- Update ---



    Oh yeah, I just realized OP is adding the JPanel to the JFrame after the JFrame has already been displayed. OP, is there a reason you're doing this?

    You have to revalidate and repaint your window for components that have been added after the JFrame is already visible.
    File directory is fine.
    I moved gameWindow.setVisible(true); to after the line gameWindow.add(splashScreen.getContent()); but it didn't make a difference.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame - ImageIcon not displaying

    First verify that your 'background' label is where you expect it to be:

    background = new JLabel(backgroundImage);
    background.setPreferredSize(new Dimension(75, 75));
    background.setBorder(new LineBorder(Color.RED));
    Run that and you should see a red square. If you do, then the problem is ImageIcon is not finding "splash_screen.png". I recommend you make a folder called 'images' inside your 'src' folder and put your images there. Then you can get them like this:

    backgroundImage = new ImageIcon(getClass().getResource("/images/splash_screen.gif"));
    This works both in the IDE and as a distributed jar, because the images are embedded inside the jar.

  12. #12
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by BinaryDigit09 View Post
    First verify that your 'background' label is where you expect it to be:

    background = new JLabel(backgroundImage);
    background.setPreferredSize(new Dimension(75, 75));
    background.setBorder(new LineBorder(Color.RED));
    Run that and you should see a red square. If you do, then the problem is ImageIcon is not finding "splash_screen.png". I recommend you make a folder called 'images' inside your 'src' folder and put your images there. Then you can get them like this:

    backgroundImage = new ImageIcon(getClass().getResource("/images/splash_screen.gif"));
    This works both in the IDE and as a distributed jar, because the images are embedded inside the jar.
    Yup there is a red square, but the image is being located. I've tested for that already. Why should putting the image in my src folder make a difference?

    Having said that there was one little thing with the file directory. THe project is in "My Documents" but the file location printed in java stated "Documents" but I'm pretty sure thats the same thing in windows.

    EDIT: I double checked, "My Documents" did indeed show as "Documents" under properties in windows 7.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: JFrame - ImageIcon not displaying

    The code works for me after I changed path to my images folder and moved the call to setVisible(true) down.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by mohagan9 View Post
    Yup there is a red square, but the image is being located. I've tested for that already. Why should putting the image in my src folder make a difference?
    I don't have "splash_screen.png" on my PC, so when I ran your code I got the same blank JFrame as you. But when I created my own "splash_screen.png" inside src/images/ and loaded it with getClass().getResource(), I got an image on the label. So based on your symptom, and mine being the same in the absence of the file, I concluded that it must not be finding the file.

    I suggested having a /src/images folder because it works the same both in the IDE and as a distributed jar. It's better to have the file in a guaranteed relative location instead of some external place like My Documents. Especially if your path to the file is relative to user.dir like this is.

    However, it works for me both ways. When I put the image next to src instead of inside it, and use new ImageIcon("splash_screen.gif") it works. Maybe you're on to something with the whole Documents vs My Documents thing?

  15. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by BinaryDigit09 View Post
    I don't have "splash_screen.png" on my PC, so when I ran your code I got the same blank JFrame as you. But when I created my own "splash_screen.png" inside src/images/ and loaded it with getClass().getResource(), I got an image on the label. So based on your symptom, and mine being the same in the absence of the file, I concluded that it must not be finding the file.

    I suggested having a /src/images folder because it works the same both in the IDE and as a distributed jar. It's better to have the file in a guaranteed relative location instead of some external place like My Documents. Especially if your path to the file is relative to user.dir like this is.

    However, it works for me both ways. When I put the image next to src instead of inside it, and use new ImageIcon("splash_screen.gif") it works. Maybe you're on to something with the whole Documents vs My Documents thing?
    Thanks for the reply, but still a blank screen

    Line is as follows:
    backgroundImage = new ImageIcon(getClass().getResource("/images/splash_screen.png"));

    My structure is as follows for Project Folder:

    >src
    >>(default package) - //java files in here//
    >>images - //splash_screen.png in here//

    >jre


    EDIT: Could there be any restrictions on file size and/or dimensions? The image is 1.4MB and 800x600.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: JFrame - ImageIcon not displaying

    If you can't change the code to have it find the image,
    try making many copies of the folder with the image putting a copy of the images folder in all the possible places that the code could be looking for it. Put it in dozens of places. When the code works, delete a folder and test. When the delete stops the code from working, that is the folder that the code is using.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by Norm View Post
    If you can't change the code to have it find the image,
    try making many copies of the folder with the image putting a copy of the images folder in all the possible places that the code could be looking for it. Put it in dozens of places. When the code works, delete a folder and test. When the delete stops the code from working, that is the folder that the code is using.
    Project Folder:

    >src
    >>(default package) - //java files here//
    >>images - //image here//

    >jre

    >images - //image here//


    Still a blank screen!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: JFrame - ImageIcon not displaying

    Still a blank screen
    Make more copies of the folder in all the possible places.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by mohagan9 View Post
    EDIT: Could there be any restrictions on file size and/or dimensions? The image is 1.4MB and 800x600.
    What happened when you tested it out with a smaller image?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFrame - ImageIcon not displaying

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tested it out with a smaller image?
    I just worked it out and I can't believe it! I'm so sorry guys haha.

    Turns out the image was actually a bmp, it just had the file extension of png. WOOOOPS!

    Thanks very much to everyone who helped out anyway

Similar Threads

  1. Update JLabel with new ImageIcon
    By iHank in forum AWT / Java Swing
    Replies: 5
    Last Post: February 6th, 2014, 08:36 AM
  2. Can't load image from folder. ImageIcon
    By kinkita in forum AWT / Java Swing
    Replies: 2
    Last Post: July 19th, 2013, 04:19 PM
  3. How to put in a "new" image in ImageIcon JFrame ?
    By craigjlner in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2013, 03:17 PM
  4. Help with ImageIcon
    By nivangerow in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 30th, 2011, 10:39 AM
  5. displaying multiple jpanels in a jframe
    By tooktook22 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 19th, 2011, 12:46 PM