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?
CodePudding user response:
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.
CodePudding user response:
https://stackoverflow.com/a/72367904/19191709
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.
Works now, thank you for helping me solve this issue!