Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4421314

JOptionPane.showInputDialog gets wrong choice with dup. text

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs



      Name: ssT124754 Date: 03/02/2001


      java version "1.3.0_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
      Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)



      Using the long constructor of JOptionPane.showInputDialog, with Strings as the
      input Objects, if initialValue.equals(anyOtherValueInList), the Object
      returned is the initialValue when the "equals" choices are selected. In
      other words, the items with equivalent strings to the initialValue cannot
      be selected.

      Here is a quick application that demonstrates the problem. It creates a
      showInputDialog with 6 entries, Object[]possibles = {"Test0", "Test0", "Test1",
      "Test1", "Test2", "Test2"} and sets initialValue to possibles[0]. It prints out
      whether the selected object is the same object and whether the strings match.
      Choose any item and you should get something like this:
      false false
      false false
      false true <--- duplicate string but not the right object
      true true <--- desired, both the object and string are matched
      false false
      false false

      However, if you choose the second item (the second "Test0"), it shows as:
      true true <--- Wrong! Should be false true
      false true <--- didn't get the right object
      ...

      I've looked in the debugger (Forte) and see the string ID's and have confirmed
      the observed behavior.

      Here's the code (sorry about word wrap formatting things...)

      public class Tester extends javax.swing.JFrame {

          public Tester() {
              initComponents ();
              pack ();
          }

          private void initComponents () {
            options = new javax.swing.JButton ();
            addWindowListener (new java.awt.event.WindowAdapter () {
              public void windowClosing (java.awt.event.WindowEvent evt) {
                exitForm (evt);
              }
            }
            );

            options.setText ("options");
            options.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                optionsActionPerformed (evt);
              }
            }
            );


            getContentPane ().add (options, java.awt.BorderLayout.CENTER);

          }

        private void optionsActionPerformed (java.awt.event.ActionEvent evt) {
          Object[] possibles = new Object[6];
          for (int j=0; j<6; j++) {
            possibles[j] = (Object)(new String("Test"+j/2));
          }

          Object selected = javax.swing.JOptionPane.showInputDialog(null,
            "Choose button to test: ","Input",
      javax.swing.JOptionPane.INFORMATION_MESSAGE,
            null,possibles,possibles[0]);

          for (int j=0; j<6; j++) {
            System.out.println
              ((selected== possibles[j])+" "+selected.equals(possibles[j]));
          }
        }
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit (0);
        }

        public static void main (String args[]) {
            new Tester ().show ();
        }

        private javax.swing.JButton options;
      }
      (Review ID: 118019)
      ======================================================================

            duke J. Duke
            ssultanasunw Shaheen Sultana (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: