0

I am having issues with creating an custom imageIcon in Java using swing. The file-name works, but it appears that the ImageIcon isn't changing at all. Here is the code:

ImageIcon icon = new ImageIcon("Hnet.com-image.png");
        JFrame frame = new JFrame("NumberPad");
        frame.setIconImage(icon.getImage());
        frame.setName("Pin Pad");
        frame.setContentPane(new NumberPad().NumberPadPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

Anyone got suggestions for what I should do?

  • Did you check the dimensions of the image you're getting from icon.getImage(). Which Icon should be changing? The one for your JFrame? What OS are you using? – matt May 24 '22 at 18:25
  • I've checked that the icon dimensions are 20 by 20. The icon I want to change is the one in the JFrame, and I am using Windows as my operating system. – Michael Byrnes May 24 '22 at 18:28
  • "I've checked that the icon dimensions are 20 by 20." How did you check that? – matt May 24 '22 at 18:31
  • Through Intellej. – Michael Byrnes May 24 '22 at 18:32
  • Try with an icon that's 16 x 16 or 32 x 32. In HTML, this is what is required (may or may not be the same in Java) – ControlAltDel May 24 '22 at 18:33
  • Tried both 16 x 16 and 32 x 32, still didn't work. – Michael Byrnes May 24 '22 at 18:37
  • Why don't you try reading your image with `ImageIO.read(...);` instead? This might work. – gthanop May 24 '22 at 18:38
  • I've updated my answer to show how you *probably* should be loading your resource file. Here is a similar question for more info. https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder – matt May 24 '22 at 18:56

1 Answers1

0

You should make a compilable example.

public class IconCheck{
    public static void main(String[] args){
        JFrame frame = new JFrame("icon test");
        ImageIcon icon = new ImageIcon("Hnet.com-image.png");
        JLabel label = new JLabel(icon);
        frame.setIconImage( icon.getImage() );
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }
}

Does that show your icon in the JFrame? If yes, then does it update your JFrame icon?

You probably want your png to be used as a resource.

ImageIcon icon = new ImageIcon( IconCheck.class.getResource("/sample.png") ); 

It looks up the path on the classpath, which intellij knows how to include.

matt
  • 10,892
  • 3
  • 22
  • 34
  • Please don't forget `icon.setImageObserver(label);`. – gthanop May 24 '22 at 18:37
  • public static void main(String[] args) { ImageIcon icon = new ImageIcon("Hnet.com-image (2).png"); JFrame frame = new JFrame("NumberPad"); frame.setIconImage(icon.getImage()); frame.setName("Pin Pad"); frame.setContentPane(new NumberPad().NumberPadPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } – Michael Byrnes May 24 '22 at 18:39
  • Code doesn't show icon in Jframe. – Michael Byrnes May 24 '22 at 18:44
  • The problem is that your code is not finding your icon. You probably want a resource. – matt May 24 '22 at 18:49
  • How would I use a resource in this situation? – Michael Byrnes May 24 '22 at 18:55