Question

I need to change the Jdialog box title bar icon. By default it uses a java coffee image.

I have searched in internet and used many codes

1. Image im = Toolkit.getDefaultToolkit().getImage("/org/qmon/generate/Images/JDialog -2.ico");
        dialog.setIconImage(im);

2. Toolkit kit = Toolkit.getDefaultToolkit ();
        Image img = kit.getImage ("/org/qmon/generate/Images/Create File Tag-16x16.png");

        dialog.setIconImage(img);

nothing works properly.. Kindly help me.. Thanks in Advance

Was it helpful?

Solution

Firtsly, ico is not a support image format for Java.

The likely reason you're having issues with the second approach is that getImage is expecting a file reference and the image you seem to referencing looks like it's embedded (stored within your application)

Try using something more like...

Image img = kit.getImage (getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));

Instead.

Personally, I prefer ImageIO.read as it throws a IOException when something goes wrong...

Image img = ImageIO.read(getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));

But that's me...

You should also consider taking a look at Convert List<BufferedImage> to Image which demonstrates the use of ico file (from a 3rd party API) and setIconImages method

OTHER TIPS

    Image image = ImageIO.read(new URL(
        "http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
    dialog.setIconImage( image );
    dialog.setVisible(true);

I am using this in my application and working fine

    java.net.URL url = ClassLoader.getSystemResource("res/java.png");
    ImageIcon icon = new ImageIcon(url);
    JOptionPane.showMessageDialog(null, jep, "UroSync",JOptionPane.INFORMATION_MESSAGE, icon);

To improve what MadProgrammer has said, I met the problem and I solved it instantiating a JDialog but using the static class Toolkit method getDefaultToolkit().getImage(Image img).

JDialog dialog = new JDialog();
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(MyMainClass.class.getResource("/myIcon.png")));

To do that you need to add before the image into the build path of the Project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top