Example usage for javax.swing JOptionPane showConfirmDialog

List of usage examples for javax.swing JOptionPane showConfirmDialog

Introduction

In this page you can find the example usage for javax.swing JOptionPane showConfirmDialog.

Prototype

public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType,
        int messageType) throws HeadlessException 

Source Link

Document

Brings up a dialog where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.

Usage

From source file:Main.java

public static void main(String[] args) {
    int response = JOptionPane.showConfirmDialog(null, "Save the file?", "Confirm  Save  Changes",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

    switch (response) {
    case JOptionPane.YES_OPTION:
        System.out.println("yes");
        break;/*from  w w w  . j  a v a2  s.  co  m*/
    case JOptionPane.NO_OPTION:
        System.out.println("no");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("cancel");
        break;
    case JOptionPane.CLOSED_OPTION:
        System.out.println("closed.");
        break;
    default:
        System.out.println("something else");
    }

}

From source file:JOptionPaneTest2.java

public static void main(String[] args) {
    JDialog.setDefaultLookAndFeelDecorated(true);
    int response = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.NO_OPTION) {
        System.out.println("No button clicked");
    } else if (response == JOptionPane.YES_OPTION) {
        System.out.println("Yes button clicked");
    } else if (response == JOptionPane.CLOSED_OPTION) {
        System.out.println("JOptionPane closed");
    }/*www  . j a  v a  2s .c om*/
}

From source file:appmain.AppMain.java

public static void main(String[] args) {

    try {//from  w  w w  . ja  va  2s  .co m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        UIManager.put("OptionPane.yesButtonText", "Igen");
        UIManager.put("OptionPane.noButtonText", "Nem");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    try {

        AppMain app = new AppMain();
        app.init();

        File importFolder = new File(DEFAULT_IMPORT_FOLDER);
        if (!importFolder.isDirectory() || !importFolder.exists()) {
            JOptionPane.showMessageDialog(null,
                    "Az IMPORT mappa nem elrhet!\n"
                            + "Ellenrizze az elrsi utat s a jogosultsgokat!\n" + "Mappa: "
                            + importFolder.getAbsolutePath(),
                    "Informci", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        File exportFolder = new File(DEFAULT_EXPORT_FOLDER);
        if (!exportFolder.isDirectory() || !exportFolder.exists()) {
            JOptionPane.showMessageDialog(null,
                    "Az EXPORT mappa nem elrhet!\n"
                            + "Ellenrizze az elrsi utat s a jogosultsgokat!\n" + "Mappa: "
                            + exportFolder.getAbsolutePath(),
                    "Informci", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        List<File> xmlFiles = app.getXMLFiles(importFolder);
        if (xmlFiles == null || xmlFiles.isEmpty()) {
            JOptionPane.showMessageDialog(null,
                    "Nincs beolvasand XML fjl!\n" + "Mappa: " + importFolder.getAbsolutePath(),
                    "Informci", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        StringBuilder fileList = new StringBuilder();
        xmlFiles.stream().forEach(xml -> fileList.append("\n").append(xml.getName()));
        int ret = JOptionPane.showConfirmDialog(null,
                "Beolvassra elksztett fjlok:\n" + fileList + "\n\nIndulhat a feldolgozs?",
                "Megtallt XML fjlok", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

        if (ret == JOptionPane.OK_OPTION) {
            String csvName = "tranzakcio_lista_" + df.format(new Date()) + "_" + System.currentTimeMillis()
                    + ".csv";
            File csv = new File(DEFAULT_EXPORT_FOLDER + "/" + csvName);
            app.writeCSV(csv, Arrays.asList(app.getHeaderLine()));
            xmlFiles.stream().forEach(xml -> {
                List<String> lines = app.readXMLData(xml);
                if (lines != null)
                    app.writeCSV(csv, lines);
            });
            if (csv.isFile() && csv.exists()) {
                JOptionPane.showMessageDialog(null,
                        "A CSV fjl sikeresen elllt!\n" + "Fjl: " + csv.getAbsolutePath(),
                        "Informci", JOptionPane.INFORMATION_MESSAGE);
                app.openFile(csv);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Feldolgozs megszaktva!", "Informci",
                    JOptionPane.INFORMATION_MESSAGE);
        }

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Nem kezelt kivtel!\n" + ExceptionUtils.getStackTrace(ex), "Hiba",
                JOptionPane.ERROR_MESSAGE);
    }

}

From source file:Main.java

public static boolean confirmAction(Component comp, String title, String msg) {
    int decision = JOptionPane.showConfirmDialog(comp, msg, title, JOptionPane.YES_NO_OPTION,
            JOptionPane.WARNING_MESSAGE);
    if (decision == JOptionPane.NO_OPTION) {
        return false;
    }/*from  w  w  w  .j a va  2 s. c o m*/
    return true;
}

From source file:Main.java

public static boolean confirmOverwrite(Component comp, File file) {
    if (file.exists()) {
        int decision = JOptionPane.showConfirmDialog(comp, file.getName() + " exists. Overwrite?",
                "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (decision == JOptionPane.NO_OPTION) {
            return false;
        }/*  w w  w . j a v  a2s  .  c  o  m*/
    }
    return true;
}

From source file:Main.java

/**
 * Displays a {@link JOptionPane} as a Confirm Dialog with OK and CANCEL buttons.
 * /*from ww w .j a  va 2 s  .  co m*/
 * @param title The title of the Dialog.
 * @param message The message of the Dialog.
 * @return "ok" or "cancel" if clicked on the respective buttons.
 */
public static String showConfirmationDialogWithOkCancelButtons(String title, String message) {
    int result = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);
    if (result == JOptionPane.OK_OPTION)
        return "ok";
    if (result == JOptionPane.CANCEL_OPTION)
        return "cancel";
    return null;
}

From source file:Main.java

/**
 * Shows a confirm dialog.//ww w.j  a  v  a2s  .c  o m
 * 
 * @param c
 *            the parent component.
 * @param msg
 *            the message to display.
 * @param messageType
 *            the type of message to display.
 * @return an int indicating the option selected by the user.
 */
public static int showConfirmDialog(Component c, String msg, int messageType) {
    return JOptionPane.showConfirmDialog(JOptionPane.getFrameForComponent(c), msg,
            UIManager.getString("OptionPane.confirmDialogTitle"), JOptionPane.YES_NO_OPTION, messageType);
}

From source file:Main.java

public static int showReplaceExistingFileConfirmDialog(Component parentComponent, File file) {
    return JOptionPane.showConfirmDialog(parentComponent,
            "Do you want to replace the existing file " + file + " ?", "Warning",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}

From source file:Main.java

/**
 * Mostra uma caixa de menssagem para que seja ensirido um valor.
 * @param frame/*from   w  w w . ja v  a2  s.  c o  m*/
 * @param texto
 * @param title
 * @param valorInicial
 * @param type
 * @return
 * @author Thiago Benega
 * @since 13/04/2009
 */
public static String showTextboxDialog(javax.swing.JFrame frame, String texto, String title,
        String valorInicial, int type) {
    Object txt = null;
    JDialog jDialog = new JDialog();
    jDialog.setTitle(title);
    jDialog.setFocusableWindowState(true);

    if (frame != null) {
        frame.setExtendedState(Frame.ICONIFIED);
        frame.pack();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    switch (type) {
    case JOptionPane.OK_CANCEL_OPTION:
        txt = JOptionPane.showInputDialog(jDialog, texto, title, type, null, null, valorInicial);//.toString();
        break;
    case JOptionPane.YES_NO_OPTION:
        txt = JOptionPane.showConfirmDialog(jDialog, texto, title, type, JOptionPane.INFORMATION_MESSAGE);
        break;
    default:
        JOptionPane.showMessageDialog(jDialog, texto, title, type);
        break;
    }

    jDialog = null;
    return txt != null ? txt.toString() : null;

}

From source file:Main.java

/**
 * Displays a confirmation window asking a user a yes or no question.
 * @param message   the message to show.
 * @param parent   Parent component of this dialog.
 * @return         true if "yes" was clicked. false otherwise.
 *//*  w w  w.  java  2s .  c  o m*/
public static boolean yesTo(String message, Component parent) {
    int c = JOptionPane.showConfirmDialog(parent, message, "Confirm", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE);
    boolean out = c == JOptionPane.YES_OPTION;
    return out;
}