Java JOptionPane.showMessageDialog problem with icon?

So, I have a popup dialog box in the application that informs the user about the program. Everything went well until the user icon. Here is what I tried:

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:

(Scream in java)

Attempt 5:

URL usage


All did not affect the program, and instead of the image I get nothing.


Details:

  • With no exceptions
  • File path exists
  • My IDE does not return exceptions, NOR any warnings of any kind
  • Yes, I also set the path /home/user/Pictures/default.jpg
  • .ico, .png, .jpg do not work. I'm not so sure about .gif right now, though.

Help me!: (

+5
source share
6

:

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);
    }
}

, 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);
    }
}
+8

, , , , ( java 1.7):

getClass().getResource(<path>) :

ImageIcon icon = new ImageIcon(getClass().getResource(<pathToIcon>));

"" , - "" , ( - , , ..)

+3

:

JPanel panel = new JPanel();
BufferedImage myPicture = null;
try
{
    myPicture = ImageIO.read(new File("home/user/Pictures/default.jpg"));
}
catch(Exception ex){}
panel.add(new JLabel(new ImageIcon(myPicture)));
panel.add(new JLabel("blah blah blah"));
Object[] options = {};
JOptionPane pane = new JOptionPane();
pane.showOptionDialog(null, panel, "About", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null);
0

The same thing happened to me, thank God, I looked that my image was not loaded in the "source" file, it was in the "bin" file .. the path was wrong

ImageIcon preg1 = new ImageIcon("C:\\Java\\TestPsicologico\\bin\\Preg1.jpg"); 
0
source

Everyone was right, since the path copied is wrong.

You just need to put the preferred image in the project folder and your image will appear on the project navigation tab, then just copy the image path and paste it into:

final ImageIcon icon = new ImageIcon("*Paste copied path*");


 JOptionPane.showMessageDialog(null, infoMessage, " " + titleBar, JOptionPane.INFORMATION_MESSAGE,icon);
0
source

All Articles