Search code examples
javaimagedialogpopupmessage

Java JOptionPane.showMessageDialog custom icon problem?


So I have a pop-up dialog in my application that tells the user about the program. Everything was going fine until the custom icon. Here's what I've attempted:

Attempt 1:

JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("home/user/Pictures/default.jpg"));

Attempt 2:

final icon = new ImageIcon("home/user/Pictures/default.jpg"));

    JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);

Attempt 3:

final icon = new ImageIcon("home/user/Pictures/default.jpg"));
showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);

Attempt 4:

(Screaming at java)

Attempt 5:

Using URL's


All have had no affect on the program and instead of an image, I don't get anything.


Details:

  • No exceptions
  • The file path DOES exist
  • My IDE doesn't return exceptions, NOR any warnings of any sort
  • Yes, I've put also tried the path /home/user/Pictures/default.jpg
  • .ico's, .png's, .jpg's don't work. I'm not so sure about .gif's right now though.

Help me! :(


Solution

  • This worked for me:

    import javax.swing.*;
    
    public class Test
    {
        public static void main(String[] args)
        {
            final ImageIcon icon = new ImageIcon("C:\\Users\\John\\Desktop\\lol.jpg");
            JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
        }
    }
    

    Here is a variant that uses a URL:

    import javax.swing.*;
    import java.net.*;
    
    public class TestIcon
    {
        public static void main(String[] args) throws Exception
        {
            final ImageIcon icon = new ImageIcon(new URL("http://www.gravatar.com/avatar/a1ab0af4997654345d7a949877f8037e?s=128&d=identicon&r=PG"));
            JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
        }
    }