Search code examples
javaswingiconsjoptionpane

Custom JOptionPane Icon


Java's "How to Make Dialogs" tutorial shows this code:

//custom title, custom icon
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.",
        "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);

which would create the following dialog:

Java's Example Dialog

Why is JOptionPane.INFORMATION_MESSAGE needed when the icon will just be changed to the icon parameter?


Solution

  • The flag also indicates which message style to use on the window decorations, see http://nadeausoftware.com/node/91#Usinglookandfeelspecificwindowdecorations

    from the JOptionPane class source code:

    private static int styleFromMessageType(int messageType) {
        switch (messageType) {
        case ERROR_MESSAGE:
            return JRootPane.ERROR_DIALOG;
        case QUESTION_MESSAGE:
            return JRootPane.QUESTION_DIALOG;
        case WARNING_MESSAGE:
            return JRootPane.WARNING_DIALOG;
        case INFORMATION_MESSAGE:
            return JRootPane.INFORMATION_DIALOG;
        case PLAIN_MESSAGE:
        default:
            return JRootPane.PLAIN_DIALOG;
        }
    }
    

    and in the method showOptionDialog which is called by showMessageDialog...

    int style = styleFromMessageType(messageType);
    JDialog dialog = pane.createDialog(parentComponent, title, style);