Java JOptionPane Confirmation confirm(final Component component, final String title, final String message)

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

Description

Asks the user a yes or no question.

License

Open Source License

Parameter

Parameter Description
component the component
title the title
message the message

Return

true if 'yes' was selected

Declaration

public static boolean confirm(final Component component, final String title, final String message) 

Method Source Code


//package com.java2s;
/*//from w  ww. j  a va 2 s .com
 * 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 {
    private static boolean confirmResult;

    /**
     * Asks the user a yes or no question.
     * 
     * @param component the component
     * @param title the title
     * @param message the message
     * @return true if 'yes' was selected
     */
    public static boolean confirm(final Component component, final String title, final String message) {
        if (!SwingUtilities.isEventDispatchThread()) {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        confirmResult = confirm(component, title, message);
                    }
                });
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }

            return confirmResult;
        }

        return JOptionPane.showConfirmDialog(component, message, title, JOptionPane.YES_NO_OPTION) == 0;
    }

    /**
     * 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. askConfirmation(final Window aWindow, final String aMessage, final String aTitle)
  2. confirm(Component component, String title, String msg)
  3. confirm(String message)
  4. confirm(String message)
  5. confirm(String message, boolean typeYesNo)
  6. confirm(String message, String title)