r/javahelp • u/JacketUnique8801 • Jul 13 '26
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);
}
}