Search code examples
javajoptionpane

JOptionPane always returning -1


I want to double check the delete operation to prevent accidental deletion.

JOptionPane

(Here the Yes button would only be enabled if the checkbox is checked.)

But both Yes and No Buttons are returning -1.

This is a snippet of my program.

public class class1 extends javax.swing.JInternalFrame {

JCheckBox cbConfirmDelete;

JPanel outer = new JPanel(new BorderLayout());
final JButton btnYes = new JButton("Yes");
final JButton btnNo = new JButton("No");

public class1() {

    ......

    //Button btnYes ActionListener for JOptionPane
    btnYes.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane pane = getOptionPane((JComponent) e.getSource());
            pane.setValue(btnYes);
        }
    });

    //Button btnNo ActionListener for JOptionPane
    btnNo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane pane = getOptionPane((JComponent) e.getSource());
            pane.setValue(btnNo);
        }
    });

    //layout for JOptionPane
    JPanel nested1 = new JPanel();
    nested1.add(cbConfirmDelete);

    JPanel nested2 = new JPanel();
    nested2.add(btnYes);
    nested2.add(btnNo);

    outer.add(nested1, BorderLayout.NORTH);
    outer.add(nested2, BorderLayout.CENTER);
}

protected JOptionPane getOptionPane(JComponent parent) {
    JOptionPane pane = null;
    if (!(parent instanceof JOptionPane)) {
        pane = getOptionPane((JComponent) parent.getParent());
    } else {
        pane = (JOptionPane) parent;
    }
    return pane;
}

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
    int dialogResult = JOptionPane.showOptionDialog(null, "Are you sure you want to Delete the Reference ?", "Delete Reference", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{outer}, btnYes);
    System.out.println("DialogResult: " + dialogResult);
}                                         
}

The Output DialogResult is always -1. Why is that happening ?

If I pass the following object in JOptionPane it works fine..

new Object[]{cbConfirmDelete, btnYes, btnNo}

But this is not working

new Object[]{outer}

Solution

  • I see a couple of problems here. Since you're creating a Yes/No option dialog, your Object[] array should contain two, and only two, components that correspond to yes or no.

    1. For your example that works, your Object[] array contains the checkbox, the yes button, and the no button. That's one too many components. When you set the pane value in the yes or no action listener, you're setting it to 1 or 2 instead of 0 or 1, since positionally, those components are the second and third. This isn't necessarily a big problem, but usually when you return from a yes/no dialog box, you check for JOptionPane.YES_OPTION (0) or JOptionPane.NO_OPTION (1) to see if the user chose yes or no. In your scenario, you're going to have to check for 1 or 2 since the checkbox is component 0.

    2. For your non-working scenario, the problem is that your object array only contains one object, the outer panel containing the checkbox and the yes/no buttons. Technically what's happening is that you're telling the option pane that there's only one selectable component, a JPanel. Calling pane.setValue() with any component other than the outer panel will have no effect, since the pane's selection value is -1 (uninitialized value) to begin with, and it will only change if you call setValue(outer).