Java tutorial
//package com.java2s; //License from project: Open Source License import javax.swing.*; import java.awt.*; import java.lang.reflect.InvocationTargetException; public class Main { public static void showMessage(final Window parent, final String message, final String title, final int messageType) { runInSwingThread(new Runnable() { @Override public void run() { //noinspection MagicConstant JOptionPane.showMessageDialog(parent, message, title, messageType); } }); } public static void runInSwingThread(final Runnable runnable) { if (SwingUtilities.isEventDispatchThread()) { runnable.run(); } else { try { SwingUtilities.invokeAndWait(runnable); } catch (final InvocationTargetException | InterruptedException e) { throw new RuntimeException(e); } } } }