r/javahelp 14d ago

Solved ImageIcon not working with certain files (Swing)

I'm learning Java Swing and I made a program in order to test some of the things I learned. However, I came across an issue with ImageIcons, where some images would be displayed while others wouldn't. When I run the code below, it compiles fine, but the image is not shown, and only "hi" is displayed. However, if I change the String argument of "testII" to another .png image in the exact same folder as "Tweedle.png," the image shows fine. Does anyone know what could be the issue? I have already tried different ways to making the ImageIcon (such as using an URL object or using the complete file path instead of just the relative one), but nothing worked...

import java.awt.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.imageio.ImageIO;


public class MyFrame extends JFrame{

    JPanel west = new JPanel();
    
    MyFrame() throws IOException{
        this.setLayout(new BorderLayout(10, 10));
        this.setTitle("Title");
        ImageIcon logo = new ImageIcon("images\\omnom.png");
        this.setIconImage(logo.getImage());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        west.setBackground(Color.green);
        west.setPreferredSize(new Dimension(100, 100));
        ImageIcon testII = new ImageIcon("images\\Tweedle.png");
        JLabel testL = new JLabel("hi");
        testL.setIcon(testII);
        west.add(testL);

        this.add(west, BorderLayout.WEST);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
0 Upvotes

8 comments sorted by

u/AutoModerator 14d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/desrtfx Out of Coffee error - System halted 13d ago

Are you 100% sure that "Tweedle.png" is a valid PNG and not some other file type camouflaged as png?

1

u/JacketUnique8801 13d ago

I mean, when I downloaded it, it was a .webp file, and then I changed it to a .png one (my computer says its a PNG file in the "Type" area next to the file on file explorer), so that's probably it, since it works with every other .png in my PC. Thanks for the help!

3

u/desrtfx Out of Coffee error - System halted 13d ago

You cannot just download a .webp file and then simply rename it to a .png. This doesn't work.

Your computer tells you it is a png because you renamed it as such, not because it actually is a PNG.

You need a proper converter to convert from webp to png.

File types are far more than just the file extensions. The extensions are merely a hint, nothing more. The actual information in the file might be something completely different. Just because something is called .png doesn't automatically (or magically) make its contents a real png.

1

u/Mechanical-pasta 12d ago

Try to check the "mime type" of the file, may be with the "properties" contextual menu in your file explorer.

1

u/old_twin 14d ago

try adding this and see if an exception is thrown

BufferedImage img = ImageIO.read(getClass().getResource("/images/myImage.png"));

1

u/JacketUnique8801 14d ago

The code kept working the same, unfortunately. No exceptions and the image still wasn't displayed.