Java Swing How to - Customize JOptionPane.showMessageDialog icon








Question

We would like to know how to customize JOptionPane.showMessageDialog icon.

Answer

Here is a variant that uses a URL:

import java.net.URL;
//from ww w  .  j  a  va 2  s.c o  m
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        final ImageIcon icon = new ImageIcon(new URL("http://www.java2s.com/style/download.png"));
        JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
    }
}

The following code uses the image from local file system.

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
//from  w ww .  j  a v  a 2s  . com
public class Main {
  public static void main(String[] args) throws Exception
    {
        final ImageIcon icon = new ImageIcon("C:/folder/location.png");
        JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
    }
}