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) throws HeadlessException 

Source Link

Document

Brings up a dialog with the options Yes, No and Cancel; with the title, Select an Option.

Usage

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new JLabel("Placeholder label"));
    pack();//from w w  w .  jav  a2 s. c  o m
    setSize(200, 200);
    setVisible(true);

    int replaced = JOptionPane.showConfirmDialog(this, "Replace existing selection?");

    String result = "?";
    switch (replaced) {
    case JOptionPane.CANCEL_OPTION:
        result = "Canceled";
        break;
    case JOptionPane.CLOSED_OPTION:
        result = "Closed";
        break;
    case JOptionPane.NO_OPTION:
        result = "No";
        break;
    case JOptionPane.YES_OPTION:
        result = "Yes";
        break;
    default:
        ;
    }
    System.out.println("Replace? " + result);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowOpened(WindowEvent e) {
        }//from   ww w .j a v  a  2  s. co m

        public void windowClosing(WindowEvent e) {
            if (JOptionPane.showConfirmDialog(null, "Are you sure ?") == JOptionPane.YES_OPTION) {
                setVisible(false);
                dispose();
            }
        }
    });
    pack();
    setVisible(true);
}

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new JLabel("Placeholder label"));
    pack();/* w  w w.j  av  a 2s.  c o  m*/
    setSize(200, 200);
    setVisible(true);

    int replaced = JOptionPane.showConfirmDialog(this, "Replace existing selection?");

    String result = "?";
    switch (replaced) {
    case JOptionPane.CANCEL_OPTION:
        result = "Canceled";
        break;
    case JOptionPane.CLOSED_OPTION:
        result = "Closed";
        break;
    case JOptionPane.NO_OPTION:
        result = "No";
        break;
    case JOptionPane.YES_OPTION:
        result = "Yes";
        break;
    default:
        ;
    }
    System.out.println("Replace? " + result);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);//from  www  . j  a v  a  2s.  c om
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button1 = new JButton("Message dialog");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog((Component) e.getSource(), "Thank you!");
        }
    });

    JButton button2 = new JButton("Input dialog");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog((Component) e.getSource(), "What is your name?");
            if (text != null && !text.equals("")) {
                JOptionPane.showMessageDialog((Component) e.getSource(), "Hello " + text);
            }
        }
    });

    JButton button3 = new JButton("Yes no dialog");
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Close this application?");
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0);
            } else if (result == JOptionPane.NO_OPTION) {
                System.out.println("Do nothing");
            }
        }
    });

    setLayout(new FlowLayout(FlowLayout.CENTER));
    getContentPane().add(button1);
    getContentPane().add(button2);
    getContentPane().add(button3);
}

From source file:com.intuit.tank.tools.debugger.SaveTextAction.java

/**
 * /*from w w  w .jav  a  2  s. c o  m*/
 */
protected void showSaveDialog() {
    int response = saveAsChooser.showSaveDialog(parent);
    if (response == JFileChooser.APPROVE_OPTION) {
        File selectedFile = saveAsChooser.getSelectedFile();
        if (selectedFile.exists()) {
            int confirm = JOptionPane.showConfirmDialog(parent,
                    "Overwrite file " + selectedFile.getName() + "?");
            if (confirm != JOptionPane.YES_OPTION) {
                return;
            }
        }
        FileWriter fw = null;
        try {
            fw = new FileWriter(selectedFile);
            writer.writeText(fw);
        } catch (Exception e) {
            System.err.println("Error writing file: " + e.toString());
            e.printStackTrace();
            JOptionPane.showMessageDialog(parent, "Error writing file: " + e.toString(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        } finally {
            IOUtils.closeQuietly(fw);
        }
    }

}

From source file:net.sf.housekeeper.HousekeeperLifecycleAdvisor.java

/**
 * Ask if user wants to save before exiting.
 */// w w  w  . j a v  a 2s . c  om
public boolean onPreWindowClose(ApplicationWindow window) {
    boolean exit = true;

    final ActionCommand saveCommand = window.getCommandManager().getActionCommand("saveCommand");

    // Only show dialog for saving before exiting if any data has been
    // changed
    if (saveCommand.isEnabled()) {
        final String question = getApplicationServices().getMessages()
                .getMessage("gui.mainFrame.saveModificationsQuestion");
        final int option = JOptionPane.showConfirmDialog(window.getControl(), question);

        // If user choses yes try to save. If that fails do not exit.
        if (option == JOptionPane.YES_OPTION) {
            saveCommand.execute();

        } else if (option == JOptionPane.CANCEL_OPTION) {
            exit = false;
        }
    }
    return exit;
}

From source file:edu.ku.brc.af.ui.PopupDlg.java

/**
 * Brings up a JOptionPane dialog with the options Yes, No and Cancel; with the title, Select an Option.  Includes a checkbox that allows the user
 * to request that this popup dialog is not shown again (uses default "do not again" message)
 * /*from ww w  .  j av  a  2s . co m*/
 * @param parent determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
 * @param message the String message to display
 * @param callingClassname the name of the class that called this method
 * @param dialogNumber a uniquely identifying id for this dialog
 * @return
 * int - an integer indicating the option selected by the user
 */
public static int showConfirmDialog(Component parent, String message, String callingClassname,
        int dialogNumber) {
    PopupDlgPrefsMgr popupMgr = new PopupDlgPrefsMgr();
    int dialogId = popupMgr.generatePopupDialogId("", message, callingClassname + dialogNumber);
    boolean dontShow = popupMgr.isDialogDisabled(dialogId);
    if (!dontShow) {
        PopupDlgContent components = new PopupDlgContent(message);
        int response = JOptionPane.showConfirmDialog(parent, components.getComponents());
        boolean disableMe = components.isCheckboxSelected();
        if (disableMe) {
            log.debug("User has selected to disable the popup");
            popupMgr.disableDialog(dialogId, response);
        } else {
            log.debug("User has selected not to disable the popup");
        }
        return response;
    }
    return popupMgr.getDisabledDialogOptionSelection(dialogId);
}

From source file:com.BuildCraft.install.InstallMain.java

public void confirmInstall() {
    boolean fileNotInstalled = false;
    for (int i = 0; i < fileList.length; i++) {
        if (!fileList[i].exists()) {
            fileNotInstalled = true;/*  ww  w . j  av  a  2 s  .  c om*/
            break;
        }
    }

    if (fileNotInstalled) {
        int option = JOptionPane.showConfirmDialog(null,
                "Part or All of BuildCraft is not installed!\nInstall?");
        if (option == JOptionPane.YES_OPTION) {
            ConsoleOut.printMsg("Installing BuildCraft...");
            doInstall();
        } else {
            ConsoleOut.printMsg("BuildCraft Installation Canceled");
            BuildCraft.cleanExit(BCExitState.NORMAL);
        }
    }
}

From source file:br.com.utfpr.pb.view.CaixasView.java

private void excluir() {
    if (JOptionPane.showConfirmDialog(this, "Tem certeza que deseja excluir?") == 0) {
        try {/*w ww .ja  v a  2s  .c  o  m*/
            controller.remove(getSelecionado());
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Ocorreu um problema ao excluir.\n\n" + e.getMessage());
        }
        initTable();
    }
}

From source file:br.com.utfpr.pb.view.PedidoVendaView.java

private void salvar() {
    if (JOptionPane.showConfirmDialog(null, "Deseja finalizar o pedido?") == 0) {
        try {//from ww w.  j  a  v a2  s .  c  o  m
            pedido.setEmissao(new Date());
            pedido.setPessoa((Pessoa) pessoa.getSelectedItem());
            pedido.getProdutos().forEach(e -> e.setPedido(pedido));
            pedido = pedidoController.save(pedido);
            /*if (JOptionPane.showConfirmDialog(null, "Pedido realizado com sucesso! Deseja imprimir?") == 0) {
            imprimePedido(pedido);
            }*/
            zerarPedido();
        } catch (Exception e) {
            if (e instanceof ConstraintViolationException) {
                JOptionPane.showMessageDialog(null,
                        ValidationUtil.getInstance().getMessage((ConstraintViolationException) e));
            } else {
                JOptionPane.showMessageDialog(null, "Ocorreu um problema ao salvar.\n\n" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}