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

Choice.insert(item,index) badly used

XMLWordPrintable

    • x86
    • windows_nt



      Name: joT67522 Date: 12/15/97


      public synchronized void insert(String item, int index) {
      if (index < 0) {
      throw new IllegalArgumentException("index less than zero.");
      }

              int nitems = getItemCount();
      Vector tempItems = new Vector();

      /* Remove the item at index, nitems-index times
      storing them in a temporary vector in the
      order they appear on the choice menu.
      */
      for (int i = index ; i < nitems; i++) {
      tempItems.addElement(getItem(index));
      remove(index);
      }

      add(item);

      /* Add the removed items back to the choice menu, they
      are already in the correct order in the temp vector.
      */
      for (int i = 0; i < tempItems.size() ; i++) {
      add((String)tempItems.elementAt(i));
      }
          }


      The problem is not actual bug, but it is a very bad implementation. The
      code now implemented has bad side effects.
      If, for example, you do an insert("Filip",0) on a choice box the code will
      first delete all the items in the choice box and after that it will add all
      the items again.
      This takes a lot of time since it's a peer implementation and will also
      result in a visual effect for the user.

      Since the Choice peer has a addItem(String,int) I suggest that you should
      use that instead since it probably implements a better algorithm.

      This code is implemented in the List component and works better and faster
      without any visual side effects.



      public void synchronized insert(String item, int index) {
      if (index < -1 || index >= items.size()) {
      index = -1;
      }
      if (index == -1) {
      items.addElement(item);
      } else {
      items.insertElementAt(item, index);
      }
      ListPeer peer = (ChoicePeer)this.peer;
      if (peer != null) {
      peer.addItem(item, index);

      }


      (Review ID: 21808)
      ======================================================================

            bchristi Brent Christian
            johsunw Joon Oh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: