Java examples for Swing:JOptionPane
Shows an question message Dialog using the given title and message.
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{ private static final Logger LOG = Logger.getLogger(DialogHelper.class); /**/*from w w w . j a va 2 s. c o m*/ * Shows an question message using the given title and message. Returns <code>true</code> if the user answered Yes, * <code>false</code> otherwise. */ public static boolean showYesNoDialog(final String title, final String message) { return showYesNoDialog(title, message, null); } /** * Shows an question message using the given title and message. Returns <code>true</code> if the user answered Yes, * <code>false</code> otherwise. */ public static boolean showYesNoDialog(final String title, final String message, final Component parentComponent) { if (SwingUtilities.isEventDispatchThread()) { return JOptionPane.YES_OPTION == JOptionPane .showConfirmDialog(parentComponent, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); } else { final int[] response = new int[] { JOptionPane.NO_OPTION }; try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { response[0] = JOptionPane.showConfirmDialog( parentComponent, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); } }); } catch (InterruptedException e) { LOG.error( "InterruptedException while trying to do SwingUtilities.invokeAndWait()", e); } catch (InvocationTargetException e) { LOG.error( "InvocationTargetException while trying to do SwingUtilities.invokeAndWait()", e); } return JOptionPane.YES_OPTION == response[0]; } } }