JDK-6428694 : JOptionPane should return null when cancel is clicked
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-05-22
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b03Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
ver winxp home svc. pack 2

A DESCRIPTION OF THE PROBLEM :
When an item in joptionpane is doubleclicked, then [cancel] is clicked the value returned is NOT null, as it should be. Instead it is as if the user had selected the [ok] button because the returned value is not null.

Works fine when item is clicked only once!


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. double click item from joptionpane (list)
2. click cancel button or [x] from title bar.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Joptiondialog should return a null value when cancel button is clicked
ACTUAL -
joptionpane returns the item that was double clicked EVEN THOUGH cancel button was pressed after double clicking item

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.lang.reflect.Array;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

class Test extends JFrame {

    public void showTestOptionDialog() {

        Object[] selectedItems = new Object[40];
        for (int i = 0; i < 39; i++) {
            selectedItems[i] = ("item :" + i);
        }
        String answer = (String) JOptionPane.showInputDialog(this,
                "Double click on selected item then click cancel",
                "Test Option Dialog", JOptionPane.WARNING_MESSAGE, null,
                selectedItems, selectedItems[0]);

        // if cancel clicked
        if (answer == null) {
            JOptionPane.showMessageDialog(null,
                    "Cancel clicked (answer should be null!), answer = " + answer,
                    null,
                    JOptionPane.INFORMATION_MESSAGE);
        
            
            System.out.println("Cancel clicked, answer = " + answer);
           // selectedItems = null;
            return;
            // else if OK clicked
        } else if (answer != null) {
            JOptionPane.showMessageDialog(null,
                    "OK clicked, answer = " + answer,
                    null,
                    JOptionPane.INFORMATION_MESSAGE);
            
            
            
            System.out.println("OK clicked, answer =" + answer);
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.showTestOptionDialog();
    }
}

---------- END SOURCE ----------

Comments
SUGGESTED FIX See webrev in the attachment.
06-06-2006

EVALUATION I can reproduce the problem in JDKs 1.6 b82, 1.5.0_6, 1.4.2_11. There are no problem in JDK 1.3.1_16. The dialog closes on double click and selected item is returned. It seems it is a regression since v1.4.
25-05-2006

SUGGESTED FIX +++ JOptionPane.java Thu May 25 16:14:48 2006 @@ -989,11 +989,12 @@ public void propertyChange(PropertyChangeEvent event) { // Let the defaultCloseOperation handle the closing // if the user closed the window without selecting a button // (newValue = null in that case). Otherwise, close the dialog. if (dialog.isVisible() && event.getSource() == JOptionPane.this && - (event.getPropertyName().equals(VALUE_PROPERTY)) && + (event.getPropertyName().equals(VALUE_PROPERTY) || + event.getPropertyName().equals(INPUT_VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { dialog.setVisible(false); } }
25-05-2006

EVALUATION I think the problem is in the following lines of code in JOptionPane: The method initDialog (988 - 1000): addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { // Let the defaultCloseOperation handle the closing // if the user closed the window without selecting a button // (newValue = null in that case). Otherwise, close the dialog. if (dialog.isVisible() && event.getSource() == JOptionPane.this && (event.getPropertyName().equals(VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { dialog.setVisible(false); } } }); In the JDK 1.3 the same code is: addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { if(dialog.isVisible() && event.getSource() == JOptionPane.this && (event.getPropertyName().equals(VALUE_PROPERTY) || event.getPropertyName().equals(INPUT_VALUE_PROPERTY))) { dialog.setVisible(false); dialog.dispose(); } } }); The propertyChange() method is called when user doubleclicks on a menu item. In this case the event property name is INPUT_VALUE_PROPERTY. As you can see, in JDK 1.3 the dialog is closed in this case, but in v1.6 dialog is not closed. The JOptionPane code change, which has caused the problem, marked in the file history as: D 1.58 00/03/27 12:40:54 seligman 98 96 00084/00068/02054 MRs: COMMENTS: Merged changes between child workspace "/net/jdk.eng/export/disk9/jndi/merlin-master" and parent workspace "/ws/jdk1.3".
25-05-2006

EVALUATION This is very wrong behavior. One of two things needs to happen: 1) Clicking cancel should return null 2) Double-clicking should choose the value and close the dialog automatically
24-05-2006