Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 204
Aaron Avatar asked Aug 26 '26 09:08

Aaron


1 Answers

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);
like image 112
GerritCap Avatar answered Aug 28 '26 22:08

GerritCap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!