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

DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)

    XMLWordPrintable

Details

    • behavioral
    • minimal
    • Java API

    Description

      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)

      Attachments

        1. webrev00.zip
          218 kB
        2. webrev01.zip
          218 kB
        3. webrev02.zip
          218 kB
        4. webrev03.zip
          229 kB
        5. webrev04.zip
          129 kB
        6. webrev05.zip
          128 kB

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: