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

JComboBox function removeAllItems destroys Vector data

XMLWordPrintable

      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 :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      If a JComboBox is intialized with a Vector array of data, and the JComboBox .removeAllItems(), function is used. The expected result is for the JComboBox to be empty, but instead the Vector is empied too.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Intialize JComboBox and Vector:

      Vector<String> records = new Vector<String>();
      records.add("record 1");
      records.add("record 2");

      JComboBox options = new JComboBox(records);

      Creating the bug
      options.removeAllItems();
      System.out.println(records.size());

      After this last command the Vector will be empty.

      This bug was in older versions of Java and other, less simplistic solutions where recommended.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expecting to have the the number 2 printed to the screen, but instead 0 is printed. This indicates the Vector is empty.
      ACTUAL -
      0 is printed

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      If I try to print an element in the Vector it throws an ArrayIndesOutOfBounds exception

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.util.*;

      public class Bug
      {
      public static void main (String args[])
      {
        Vector<String> records = new Vector<String>();
      records.add("record 1");
      records.add("record 2");

      JComboBox options = new JComboBox(records);

      System.out.println("Size of Vector before removeAllItems from JComboBox: "+records.size());
      options.removeAllItems();
      System.out.println("Size of Vector after removeAllItems from JComboBox: "+records.size());
      System.out.println(records.get(0));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      When intializing the JComboBox use the Vector.toArray() function.

      ie. JComboBox options = new JComboBox(records.toArray());

      If you want to add new elements to the JComboBox using the addItems() funtion I create a temp. array of type Object and use the Vector.toArray function and then add the elements from the array.

        Fixed version of test case:
      import javax.swing.*;
      import java.util.*;

      public class Bug
      {
      public static void main (String args[])
      {
        Vector<String> records = new Vector<String>();
      records.add("record 1");
      records.add("record 2");

      JComboBox options = new JComboBox(records.toArray());

      System.out.println("Size of Vector before removeAllItems from JComboBox: "+records.size());
      options.removeAllItems();
      System.out.println("Size of Vector after removeAllItems from JComboBox: "+records.size());
      System.out.println(records.get(0));
      }
      }

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: