user569322
user569322

Reputation:

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:

Help me! :(

Upvotes: 8

Views: 57564

Answers (6)

Nofal
Nofal

Reputation: 370

Everyone was right it's just that the path copied is incorrect.

You just have to place the preferred image in your project's folder and your image will show up in the project navigation tab , after that 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);

Upvotes: 0

Hagai
Hagai

Reputation: 703

I know this is a little old, but as there was no reply that solved my question, after some research this is what worked for me (working with java 1.7):

I have used the getClass().getResource(<path>) method like this:

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

It seems to me as a good practice to create a 'resources' folder in your project, and inside it a 'icons' folder, and to refer to that location every-time you need an icon (or anything else such as audio files, images etc)

Upvotes: 3

Carrillo
Carrillo

Reputation: 1

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

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

Upvotes: 0

John Kurlak
John Kurlak

Reputation: 6680

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

Upvotes: 10

Eng.Fouad
Eng.Fouad

Reputation: 117587

Try this:

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

Upvotes: 0

Related Questions