0

I could not see the icon with those lines of codes, although it should work. The code is the following:

    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    public class IconOfDialog {
    
        public static void main(String[] args) {
    
            ImageIcon orignalIcon = new ImageIcon("/Images/icons8-ok-100.png");
            Image iconImage = orignalIcon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
            ImageIcon okIcon = new ImageIcon(iconImage);
            
            JOptionPane.showConfirmDialog(null, "Successfully connect to the database",
                                "Successful Connection", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, okIcon);
        }
    
    }

Result with no exceptions:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RedRabbit
  • 31
  • 1
  • 7
  • To put it simply...The image file can not be found, has the wrong path, or file name, or is corrupt. Is the directory your image is in actually named `Images` or is it named `images` (all lower letter case). Try to use the actual full path to the image and see if that works. The `new ImageIcon("somefile")` does NOT throw an Exeception if it can't find the file, it just returns null. [Give this SO Post a thorough read](https://stackoverflow.com/questions/38901893/what-happens-when-a-path-for-imageiconstring-is-invalid-java) – DevilsHnd - 退職した Jan 08 '22 at 11:56

1 Answers1

0

Your code is working for me, please make sure the name or path to the image is correct.

Click to view the screenshot

Ous Mass
  • 1
  • 2