Popup a message in a dialog on the event-dispatch thread. - Java Swing

Java examples for Swing:JDialog

Description

Popup a message in a dialog on the event-dispatch thread.

Demo Code


import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;

public class Main{
    /**/*www  .j  ava 2  s  . c o  m*/
     * Popup a message in a dialog on the event-dispatch thread.
     * @param message   The mesasge to popup.
     */
    @SuppressWarnings("synthetic-access")
    public static void popup(String message) {
        if (SwingUtilities.isEventDispatchThread()) { // we are in the event-dispatch thread
            popupMessage(message);
        } else { // not the event-dispatch thread
            SwingUtilities.invokeLater(new PopupRunnable(message));
        }
    }
    /**
     * Popup information related to a <code>Throwable</code>.
     * @param t   The throwable to display.
     */
    public static void popup(Throwable t) {
        t.printStackTrace();

        String message = t.getMessage();

        if (t.getCause() != null) {
            if ((message != null) && (message.length() == 0)) {
                message += " caused by ";
            }
            message += t.getCause().getMessage();
        }

        popup(message);
    }
    /**
     * Popup a message dialog.
     * @param message   The message to display.
     */
    private static void popupMessage(String message) {
        JOptionPane.showMessageDialog(null, message, "Problem",
                JOptionPane.ERROR_MESSAGE);
    }
}

Related Tutorials