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

DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)

XMLWordPrintable

    • behavioral
    • minimal
    • Java API

      Summary

      Add methods addAll, which adds a set of elements in a collection to the JList and JComboBox.

      Problem

      There is currently no straight forward way of adding a Collection to a DefaultListModel and DefaultComboBoxModel. Shown below is the typical way clients add multiple elements to the model:

      DefaultListModel lm = new DefaultListModel<>(); List lst = IntStream.range(0, 50).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

      for (int i =0; i < lst.size(); i++) { lm.add(i); }

      The above method requires more code from the client side, and also results in unnecessary events being generated for each insertion.

      Solution

      Solution is to add new api addAll, that takes a collection of items and add them all at once. Here is the webrev containing the proposed changes: http://cr.openjdk.java.net/~kaddepalli/4842658/webrev05/

      Specification

      Providing the public methods that are being affected here:

      1. src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java
          /**
           * Adds all of the elements present in the collection.
           * 
           * @param c the collection which contains the elements to add
           * @throws NullPointerException if {@code c} is null
           */
          public void addAll(Collection<? extends E> c)
      
          /**
           * Adds all of the elements present in the collection, starting
           * from the specified index.
           * 
           * @param index index at which to insert the first element from the
           * specified collection
           * @param c the collection which contains the elements to add
           * @throws ArrayIndexOutOfBoundsException if {@code index} does not 
           * fall within the range of number of elements currently held
           * @throws NullPointerException if {@code c} is null
           */
          public void addAll(int index, Collection<? extends E> c)

      2. src/java.desktop/share/classes/javax/swing/DefaultListModel.java

          /**
           * Adds all of the elements present in the collection to the list.
           *
           * @param c the collection which contains the elements to add
           * @throws NullPointerException if {@code c} is null
           */
          public void addAll(Collection<? extends E> c)
      
          /**
           * Adds all of the elements present in the collection, starting
           * from the specified index.
           * 
           * @param index index at which to insert the first element from the 
           * specified collection
           * @param c the collection which contains the elements to add
           * @throws ArrayIndexOutOfBoundsException if {@code index} does not
           * fall within the range of number of elements currently held
           * @throws NullPointerException if {@code c} is null
           */
          public void addAll(int index, Collection<? extends E> c)

            kaddepalli Krishna Addepalli
            jkimsunw Jeffrey Kim (Inactive)
            Sergey Bylokhov
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: