Java Swing How to - Change the JOptionPane layout, like the color at the top and the image at the top left








Question

We would like to know how to change the JOptionPane layout, like the color at the top and the image at the top left.

Answer

import java.awt.Color;
/*from w ww. j a  v a  2 s . c om*/
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
  public static void main(String... args) throws Exception {
    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.RED);

    java.net.URL url = new java.net.URL(
        "http://www.java2s.com/style/download.png");
    ImageIcon image = new ImageIcon(url);
    JLabel label = new JLabel("LABEL", image, JLabel.RIGHT);
    panel.add(label);

    JOptionPane.showMessageDialog(null, panel, "Modified JOptionPane : ",
        JOptionPane.PLAIN_MESSAGE);
  }

}