Java JOptionPane Error showError(final Component component, final String title, final String message)

Here you can find the source of showError(final Component component, final String title, final String message)

Description

Shows an error dialog.

License

Open Source License

Parameter

Parameter Description
component component
title title
message message

Declaration

public static void showError(final Component component, final String title, final String message) 

Method Source Code


//package com.java2s;
/*//from  w w  w.java2  s .co  m
 * SK's Minecraft Launcher
 * Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Component;

import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Main {
    /**
     * Shows an error dialog.
     * 
     * <p>This can be called from a different thread from the event dispatch
     * thread, and it will be made thread-safe.</p>
     * 
     * @param component component
     * @param title title
     * @param message message
     */
    public static void showError(final Component component, final String title, final String message) {
        if (!SwingUtilities.isEventDispatchThread()) {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        showError(component, title, message);
                    }
                });
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            return;
        }

        String newMessage = message.replace(">", "&gt;").replace("<", "&lt;").replace("&", "&amp;");
        newMessage = "<html>" + newMessage;

        JOptionPane.showMessageDialog(component, newMessage, title, JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Invoke, wait, and ignore errors.
     * 
     * @param runnable a runnable
     */
    public static void invokeAndWait(Runnable runnable) {
        try {
            SwingUtilities.invokeAndWait(runnable);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. showError(Component parentComponent, Exception e)
  2. showError(Component where, String msg)
  3. showError(Exception e)
  4. showError(Exception e)
  5. showError(Exception e)
  6. showError(final Component rootComponent, final String message, final String title)
  7. showError(JComponent parent, Throwable e)
  8. showError(JFrame owner, ImageIcon image, String title, String message, String text)
  9. showError(String errorMsg)