Shows an error message Dialog using the given title and message. - Java Swing

Java examples for Swing:JOptionPane

Description

Shows an error message Dialog using the given title and message.

Demo Code


import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

public class Main{
    /** Shows an error message using the given title and message. */
    public static void showErrorMessage(final String title,
            final String message) {
        showErrorMessage(title, message, null);
    }/*from  ww w  . j  a v a  2s. c om*/
    /** Shows an error message using the given title and message. */
    public static void showErrorMessage(final String title,
            final String message, final Component parentComponent) {
        showMessageDialog(title, message, JOptionPane.ERROR_MESSAGE,
                parentComponent);
    }
    /** Shows an informational message using the given title and message. */
    private static void showMessageDialog(final String title,
            final String message, final int messageType,
            final Component parentComponent) {
        if (SwingUtilities.isEventDispatchThread()) {
            JOptionPane.showMessageDialog(parentComponent, message, title,
                    messageType);
        } else {
            SwingUtils.runInGUIThread(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(parentComponent, message,
                            title, messageType);
                }
            });
        }
    }
}

Related Tutorials