Example usage for javax.swing JOptionPane YES_NO_OPTION

List of usage examples for javax.swing JOptionPane YES_NO_OPTION

Introduction

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

Prototype

int YES_NO_OPTION

To view the source code for javax.swing JOptionPane YES_NO_OPTION.

Click Source Link

Document

Type used for showConfirmDialog.

Usage

From source file:misc.ModalityDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method is invoked from the/*from  w w  w .  j  a  v  a 2s . c om*/
 * event-dispatching thread.
 */
private void createAndShowGUI() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(gc);
    int sw = gc.getBounds().width - ins.left - ins.right;
    int sh = gc.getBounds().height - ins.top - ins.bottom;

    // first document

    // frame f1

    f1 = new JFrame("Book 1 (parent frame)");
    f1.setBounds(32, 32, 300, 200);
    f1.addWindowListener(closeWindow);
    // create radio buttons
    rb11 = new JRadioButton("Biography", true);
    rb12 = new JRadioButton("Funny tale", false);
    rb13 = new JRadioButton("Sonnets", false);
    // place radio buttons into a single group
    ButtonGroup bg1 = new ButtonGroup();
    bg1.add(rb11);
    bg1.add(rb12);
    bg1.add(rb13);
    JButton b1 = new JButton("OK");
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // get label of selected radiobutton
            String title = null;
            if (rb11.isSelected()) {
                title = rb11.getText();
            } else if (rb12.isSelected()) {
                title = rb12.getText();
            } else {
                title = rb13.getText();
            }
            // prepend radio button label to dialogs' titles
            d2.setTitle(title + " (modeless dialog)");
            d3.setTitle(title + " (document-modal dialog)");
            d2.setVisible(true);
        }
    });
    Container cp1 = f1.getContentPane();
    // create three containers to improve layouting
    cp1.setLayout(new GridLayout(1, 3));
    // an empty container
    Container cp11 = new Container();
    // a container to layout components
    Container cp12 = new Container();
    // an empty container
    Container cp13 = new Container();
    // add a button into a separate panel
    JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout());
    p1.add(b1);
    // add radio buttons and the OK button one after another into a single column
    cp12.setLayout(new GridLayout(4, 1));
    cp12.add(rb11);
    cp12.add(rb12);
    cp12.add(rb13);
    cp12.add(p1);
    // add three containers
    cp1.add(cp11);
    cp1.add(cp12);
    cp1.add(cp13);

    // dialog d2

    d2 = new JDialog(f1);
    d2.setBounds(132, 132, 300, 200);
    d2.addWindowListener(closeWindow);
    JLabel l2 = new JLabel("Enter your name: ");
    l2.setHorizontalAlignment(SwingConstants.CENTER);
    tf2 = new JTextField(12);
    JButton b2 = new JButton("OK");
    b2.setHorizontalAlignment(SwingConstants.CENTER);
    b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //pass a name into the document modal dialog
            l3.setText("by " + tf2.getText());
            d3.setVisible(true);
        }
    });
    Container cp2 = d2.getContentPane();
    // add label, text field and button one after another into a single column
    cp2.setLayout(new BorderLayout());
    cp2.add(l2, BorderLayout.NORTH);
    cp2.add(tf2, BorderLayout.CENTER);
    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());
    p2.add(b2);
    cp2.add(p2, BorderLayout.SOUTH);

    // dialog d3

    d3 = new JDialog(d2, "", Dialog.ModalityType.DOCUMENT_MODAL);
    d3.setBounds(232, 232, 300, 200);
    d3.addWindowListener(closeWindow);
    JTextArea ta3 = new JTextArea();
    l3 = new JLabel();
    l3.setHorizontalAlignment(SwingConstants.RIGHT);
    Container cp3 = d3.getContentPane();
    cp3.setLayout(new BorderLayout());
    cp3.add(new JScrollPane(ta3), BorderLayout.CENTER);
    JPanel p3 = new JPanel();
    p3.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p3.add(l3);
    cp3.add(p3, BorderLayout.SOUTH);

    // second document

    // frame f4

    f4 = new JFrame("Book 2 (parent frame)");
    f4.setBounds(sw - 300 - 32, 32, 300, 200);
    f4.addWindowListener(closeWindow);
    // create radio buttons
    rb41 = new JRadioButton("Biography", true);
    rb42 = new JRadioButton("Funny tale", false);
    rb43 = new JRadioButton("Sonnets", false);
    // place radio buttons into a single group
    ButtonGroup bg4 = new ButtonGroup();
    bg4.add(rb41);
    bg4.add(rb42);
    bg4.add(rb43);
    JButton b4 = new JButton("OK");
    b4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // get label of selected radiobutton
            String title = null;
            if (rb41.isSelected()) {
                title = rb41.getText();
            } else if (rb42.isSelected()) {
                title = rb42.getText();
            } else {
                title = rb43.getText();
            }
            // prepend radiobutton label to dialogs' titles
            d5.setTitle(title + " (modeless dialog)");
            d6.setTitle(title + " (document-modal dialog)");
            d5.setVisible(true);
        }
    });
    Container cp4 = f4.getContentPane();
    // create three containers to improve layouting
    cp4.setLayout(new GridLayout(1, 3));
    Container cp41 = new Container();
    Container cp42 = new Container();
    Container cp43 = new Container();
    // add the button into a separate panel
    JPanel p4 = new JPanel();
    p4.setLayout(new FlowLayout());
    p4.add(b4);
    // add radiobuttons and the OK button one after another into a single column
    cp42.setLayout(new GridLayout(4, 1));
    cp42.add(rb41);
    cp42.add(rb42);
    cp42.add(rb43);
    cp42.add(p4);
    //add three containers
    cp4.add(cp41);
    cp4.add(cp42);
    cp4.add(cp43);

    // dialog d5

    d5 = new JDialog(f4);
    d5.setBounds(sw - 400 - 32, 132, 300, 200);
    d5.addWindowListener(closeWindow);
    JLabel l5 = new JLabel("Enter your name: ");
    l5.setHorizontalAlignment(SwingConstants.CENTER);
    tf5 = new JTextField(12);
    tf5.setHorizontalAlignment(SwingConstants.CENTER);
    JButton b5 = new JButton("OK");
    b5.setHorizontalAlignment(SwingConstants.CENTER);
    b5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //pass a name into the document modal dialog
            l6.setText("by " + tf5.getText());
            d6.setVisible(true);
        }
    });
    Container cp5 = d5.getContentPane();
    // add label, text field and button one after another into a single column
    cp5.setLayout(new BorderLayout());
    cp5.add(l5, BorderLayout.NORTH);
    cp5.add(tf5, BorderLayout.CENTER);
    JPanel p5 = new JPanel();
    p5.setLayout(new FlowLayout());
    p5.add(b5);
    cp5.add(p5, BorderLayout.SOUTH);

    // dialog d6

    d6 = new JDialog(d5, "", Dialog.ModalityType.DOCUMENT_MODAL);
    d6.setBounds(sw - 500 - 32, 232, 300, 200);
    d6.addWindowListener(closeWindow);
    JTextArea ta6 = new JTextArea();
    l6 = new JLabel();
    l6.setHorizontalAlignment(SwingConstants.RIGHT);
    Container cp6 = d6.getContentPane();
    cp6.setLayout(new BorderLayout());
    cp6.add(new JScrollPane(ta6), BorderLayout.CENTER);
    JPanel p6 = new JPanel();
    p6.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p6.add(l6);
    cp6.add(p6, BorderLayout.SOUTH);

    // third document

    // frame f7

    f7 = new JFrame("Classics (excluded frame)");
    f7.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
    f7.setBounds(32, sh - 200 - 32, 300, 200);
    f7.addWindowListener(closeWindow);
    JLabel l7 = new JLabel("Famous writers: ");
    l7.setHorizontalAlignment(SwingConstants.CENTER);
    // create radio buttons
    rb71 = new JRadioButton("Burns", true);
    rb72 = new JRadioButton("Dickens", false);
    rb73 = new JRadioButton("Twain", false);
    // place radio buttons into a single group
    ButtonGroup bg7 = new ButtonGroup();
    bg7.add(rb71);
    bg7.add(rb72);
    bg7.add(rb73);
    Container cp7 = f7.getContentPane();
    // create three containers to improve layouting
    cp7.setLayout(new GridLayout(1, 3));
    Container cp71 = new Container();
    Container cp72 = new Container();
    Container cp73 = new Container();
    // add the label into a separate panel
    JPanel p7 = new JPanel();
    p7.setLayout(new FlowLayout());
    p7.add(l7);
    // add a label and radio buttons one after another into a single column
    cp72.setLayout(new GridLayout(4, 1));
    cp72.add(p7);
    cp72.add(rb71);
    cp72.add(rb72);
    cp72.add(rb73);
    // add three containers
    cp7.add(cp71);
    cp7.add(cp72);
    cp7.add(cp73);

    // fourth document

    // frame f8

    f8 = new JFrame("Feedback (parent frame)");
    f8.setBounds(sw - 300 - 32, sh - 200 - 32, 300, 200);
    f8.addWindowListener(closeWindow);
    JButton b8 = new JButton("Rate yourself");
    b8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showConfirmDialog(null, "I really like my book", "Question (application-modal dialog)",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        }
    });
    Container cp8 = f8.getContentPane();
    cp8.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 8));
    cp8.add(b8);
}

From source file:com.dragoniade.deviantart.favorites.FavoritesDownloader.java

private boolean nextDeviation(int skipped) {

    progress.incremTotal();//w ww . j a v a2 s . c o  m
    progress.setUnitValue(1);
    progress.setUnitMax(Integer.MAX_VALUE);

    if (skipped == 480) {
        int res = showConfirmDialog(owner, "480 deviations have been skipped so far. Continue scanning?",
                "Continue?", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.NO_OPTION) {
            return false;
        }
    }

    if (requestCount > 20) {
        throttle();
    }
    return true;
}

From source file:fll.scheduler.SchedulerUI.java

private void newScheduleDescription() {
    final int result = JOptionPane.showConfirmDialog(SchedulerUI.this,
            "This action will remove any changes to the current schedule and load the defaults. Do you want to continue?",
            "Question", JOptionPane.YES_NO_OPTION);
    if (JOptionPane.YES_OPTION == result) {
        mScheduleDescriptionFile = null;
        mDescriptionFilename.setText("");

        final SolverParams params = new SolverParams();
        mScheduleDescriptionEditor.setParams(params);
    }//from   w  w  w .j  a  v  a 2 s . c o m
}

From source file:org.obiba.onyx.jade.instrument.ricelake.RiceLakeWeightInstrumentRunner.java

/**
 * Displays a confirmation window when the application is closed by the user without saving.
 *//*ww  w .ja v a2 s.  co m*/
protected void confirmOnExit() {

    // Ask for confirmation only if data has been fetch from the device.
    if (saveButton.isEnabled()) {

        int wConfirmation = JOptionPane.showConfirmDialog(appWindow,
                resourceBundle.getString("Confirmation.Close_window"),
                resourceBundle.getString("Title.Confirmation"), JOptionPane.YES_NO_OPTION);

        // If confirmed, application is closed.
        if (wConfirmation == JOptionPane.YES_OPTION) {
            exitUI();
        }

    } else {
        exitUI();
    }
}

From source file:gmgen.util.MiscUtilities.java

/**
 * Copy a file/*from   w w w .  ja v a  2 s  .  com*/
 * @param from_file
 * @param to_file
 * @throws IOException
 */
public static void copy(File from_file, File to_file) throws IOException {
    // First make sure the source file exists, is a file, and is readable.
    if (!from_file.exists()) {
        throw new IOException("FileCopy: no such source file: " + from_file.getPath());
    }

    if (!from_file.isFile()) {
        throw new IOException("FileCopy: can't copy directory: " + from_file.getPath());
    }

    if (!from_file.canRead()) {
        throw new IOException("FileCopy: source file is unreadable: " + from_file.getPath());
    }

    // If the destination is a directory, use the source file name
    // as the destination file name
    if (to_file.isDirectory()) {
        to_file = new File(to_file, from_file.getName());
    }

    // If the destination exists, make sure it is a writeable file
    // and ask before overwriting it.  If the destination doesn't
    // exist, make sure the directory exists and is writeable.
    if (to_file.exists()) {
        if (!to_file.canWrite()) {
            throw new IOException("FileCopy: destination file is unwriteable: " + to_file.getPath());
        }

        // Ask whether to overwrite it
        int choice = JOptionPane.showConfirmDialog(null, "Overwrite existing file " + to_file.getPath(),
                "File Exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

        if (choice != JOptionPane.YES_OPTION) {
            throw new IOException("FileCopy: existing file was not overwritten.");
        }
    } else {
        // if file doesn't exist, check if directory exists and is writeable.
        // If getParent() returns null, then the directory is the current dir.
        // so look up the user.dir system property to find out what that is.
        String parent = to_file.getParent(); // Get the destination directory

        if (parent == null) {
            parent = Globals.getDefaultPath(); // or CWD
        }

        File dir = new File(parent); // Convert it to a file.

        if (!dir.exists()) {
            throw new IOException("FileCopy: destination directory doesn't exist: " + parent);
        }

        if (dir.isFile()) {
            throw new IOException("FileCopy: destination is not a directory: " + parent);
        }

        if (!dir.canWrite()) {
            throw new IOException("FileCopy: destination directory is unwriteable: " + parent);
        }
    }

    // If we've gotten this far, then everything is okay.
    // So we copy the file, a buffer of bytes at a time.
    FileInputStream from = null; // Stream to read from source
    FileOutputStream to = null; // Stream to write to destination

    try {
        from = new FileInputStream(from_file); // Create input stream
        to = new FileOutputStream(to_file); // Create output stream

        byte[] buffer = new byte[4096]; // A buffer to hold file contents
        int bytes_read; // How many bytes in buffer

        while ((bytes_read = from.read(buffer)) != -1) { // Read bytes until EOF
            to.write(buffer, 0, bytes_read); //   write bytes
        }
    }

    // Always close the streams, even if exceptions were thrown
    finally {
        if (from != null) {
            try {
                from.close();
            } catch (IOException e) {
                //TODO: Should this really be ignored?
            }
        }

        if (to != null) {
            try {
                to.close();
            } catch (IOException e) {
                //TODO: Should this really be ignored?
            }
        }
    }
}

From source file:wsattacker.plugin.intelligentdos.ui.dialog.Result_NB.java

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveBtnActionPerformed
    try {//from  w w  w  .  j a v a2  s . c  o m
        JFileChooser fileChooser = new JFileChooser();
        int checker = fileChooser.showOpenDialog(null);
        if (checker == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            String message = "The selected file already exists.\nOverwrite?";
            if (!selectedFile.exists() || JOptionPane.showConfirmDialog(this, message, "Save result",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {

                if (!FilenameUtils.getExtension(selectedFile.getName()).equalsIgnoreCase(FILE_EXT_ZIP)) {
                    selectedFile = new File(selectedFile.getAbsoluteFile() + "." + FILE_EXT_ZIP);
                }

                btnController.save(selectedFile);
                // default title and icon
                JOptionPane.showMessageDialog(this, "Result has been saved to filesystem.");
            }
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, e.getMessage(), "Fehler beim Schreiben", JOptionPane.ERROR_MESSAGE);
        logger.warn(e, e);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.openbravo.pos.admin.RolesViewTree.java

/**
 *
 * @return @throws BasicException/*from  w w w  .j  av a  2s . c o m*/
 */
@Override
public Object createValue() throws BasicException {
    Object[] role = new Object[4];
    role = new Object[4];
    role[0] = m_oId == null ? UUID.randomUUID().toString() : m_oId;
    role[1] = m_jName.getText();
    role[2] = Formats.BYTEA.parseValue(buildPermissionsStr());
    role[3] = jRightsLevel.getValue();

    if (!hasPermissions) {
        Object[] options = { AppLocal.getIntString("Button.NoPermissionsYes"),
                AppLocal.getIntString("Button.NoPermissionsNo") };
        if (JOptionPane.showOptionDialog(this,
                AppLocal.getIntString("Message.adminpermissions1") + m_jName.getText() + " "
                        + AppLocal.getIntString("Message.adminpermissions2"),
                AppLocal.getIntString("Message.adminwarning"), JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE, null, options, options[1]) == 1) {
            // Re-instate original permissions for this role
            role = new Object[4];
            role[0] = m_oId == null ? UUID.randomUUID().toString() : m_oId;
            role[1] = m_jName.getText();
            role[2] = Formats.BYTEA.parseValue(m_dlAdmin.findRolePermissions(role[0].toString()));
            role[3] = m_dlAdmin.getRightsLevel(m_jName.getText());
            return role;
        }
    }
    return role;
}

From source file:com.holycityaudio.SpinCAD.SpinCADFile.java

public void fileSavePatchAs(SpinCADPatch p) {
    // Create a file chooser
    String savedPath = prefs.get("MRUPatchFolder", "");
    final JFileChooser fc = new JFileChooser(savedPath);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("SpinCAD Files", "spcd");
    fc.setFileFilter(filter);//from ww  w .  j  av a2  s  . com
    fc.setSelectedFile(new File(p.patchFileName));
    int returnVal = fc.showSaveDialog(new JFrame());
    // need to process user canceling box right here
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        // In response to a button click:
        File fileToBeSaved = fc.getSelectedFile();

        if (!fileToBeSaved.getAbsolutePath().endsWith(".spcd")) {
            fileToBeSaved = new File(fc.getSelectedFile() + ".spcd");
        }
        int n = JOptionPane.YES_OPTION;
        if (fileToBeSaved.exists()) {
            JFrame frame = new JFrame();
            n = JOptionPane.showConfirmDialog(frame, "Would you like to overwrite it?", "File already exists!",
                    JOptionPane.YES_NO_OPTION);
        }
        if (n == JOptionPane.YES_OPTION) {
            try {
                String filePath = fileToBeSaved.getPath();
                String fileName = fileToBeSaved.getName();
                p.patchFileName = fileName;
                fileSavePatch(p);
                recentPatchFileList.add(fileToBeSaved);
                saveMRUPatchFolder(filePath);

            } catch (Exception e) { // thrown over in SpinCADFile.java
                e.printStackTrace();
                SpinCADDialogs.MessageBox("File save failed!", "look at stack trace for info");
            } finally {
                p.setChanged(false);
            }
        }
    }
}

From source file:fr.vdl.android.holocolors.HoloColorsDialog.java

private void checkLicence() {
    try {//  w  ww .  j ava 2 s.  c o m
        String userHome = System.getProperty("user.home");
        File holoColorsFolder = new File(userHome + File.separator + ".holocolors");
        File licenceFile = new File(holoColorsFolder, ".licence");
        File noDonationFile = new File(holoColorsFolder, ".nodonation");

        if (noDonationFile.exists()) {
            return;
        }

        int usage = 1;
        boolean showPopup = false;
        if (!holoColorsFolder.exists()) {
            holoColorsFolder.mkdir();
            showPopup = true;
            licenceFile.createNewFile();
        } else {
            Scanner in = new Scanner(new FileReader(licenceFile));
            if (in.hasNextInt()) {
                usage = in.nextInt() + 1;
            }
            in.close();
        }
        if (usage > 10) {
            usage = 1;
            showPopup = true;
        }
        Writer out = new BufferedWriter(new FileWriter(licenceFile));
        out.write(String.valueOf(usage));
        out.close();

        if (showPopup) {
            Object[] donationOption = { "Make a donation", "Maybe later", "No Never" };
            int option = JOptionPane.showOptionDialog(ahcPanel,
                    "Thanks for using Android Holo Colors!\n\nAndroid Holo Colors (website and plugin) is free to use.\nIf you save time and money with it, please make a donation.",
                    "Support Android Holo Colors", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                    new ImageIcon(getClass().getResource("/icons/H64.png")), donationOption, donationOption[0]);
            if (option == 0) {
                openWebpage(
                        "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XQSBX55A2Z46U");
            }
            if (option == 2) {
                noDonationFile.createNewFile();
            }
        }
    } catch (Exception e) {
        // no matter, nothing to do
        e.printStackTrace();
    }
}

From source file:com.biosis.biosislite.vistas.inventario.MantenimientoTipo.java

private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
    // TODO add your handling code here:
    accion = AbstractControlador.ELIMINAR;
    if (tbltipo.getSelectedRow() != -1) {

        Integer codigo = tbltipo.getSelectedRow();

        Tipo tipo = tipoControlador.buscarPorId(lista.get(codigo).getId());

        if (tipo != null) {
            if (JOptionPane.showConfirmDialog(null, "Desea Eliminar el Tipo?", "Mensaje del Sistema",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {

                int[] filas = tbltipo.getSelectedRows();
                for (int i = 0; i < filas.length; i++) {
                    Tipo empleado2 = lista.get(filas[0]);
                    lista.remove(empleado2);
                    tipoControlador.setSeleccionado(empleado2);
                    tipoControlador.accion(accion);
                }/*from   w ww. j  a  v  a  2 s  .c  o m*/
                if (tipoControlador.accion(accion) == 3) {
                    JOptionPane.showMessageDialog(null, "Tipo eliminado correctamente", "Mensaje del Sistema",
                            JOptionPane.INFORMATION_MESSAGE);

                } else {
                    JOptionPane.showMessageDialog(null, "Tipo no eliminada", "Mensaje del Sistema",
                            JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Tipo no eliminada", "Mensaje del Sistema",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    } else {
        JOptionPane.showMessageDialog(null, "Debe seleccionar un Tipo", "Mensaje del Sistema",
                JOptionPane.ERROR_MESSAGE);
    }
}