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

Add getSelectedIndices() to ListSelectionModel

    Details

    • Subcomponent:
    • Resolved In Build:
      b08
    • CPU:
      x86
    • OS:
      generic

      Description

      A DESCRIPTION OF THE REQUEST :
      Currently,

      * JList has a method getSelectedIndices(), which gets the indices from its ListSelectionModel.
      * JTable has a method getSelectedRows(), which gets the indices from its ListSelectionModel.
      * DefaultTableColumnModel has a method getSelectedColumns(), which gets the indices from its ListSelectionModel.

      I believe it makes a lot of sense to have getSelectedIndices() on ListSelectionModel itself, which is happily possible now that default methods are allowed on interfaces.

      An implementation (based on the existing ones but tidied up a bit):

          /**
           * Returns an array of all of the selected indices, in increasing order.
           *
           * @return all of the selected indices, in increasing order,
           * or an empty array if nothing is selected
           */
          default int[] getSelectedIndices() {
              int iMin = getMinSelectionIndex();
              int iMax = getMaxSelectionIndex();
              
              if (iMin == -1) return new int[0];
              
              int[] indices = new int[(iMax - iMin) + 1];
              int count = 0;
              for (int i = iMin; i <= iMax; i++) {
                  if (isSelectedIndex(i)) {
                      indices[count++] = i;
                  }
              }
              
              return Arrays.copyOf(indices, count);
          }

      JUSTIFICATION :
      1. JList, JTable, and DefaultTableColumnModel currently have separately maintained, but mostly identical versions of the method. It makes sense to factor out the common code.

      2. The method is useful to other components. (This is exactly the situation in my application. Even though there are three existing implementations of the method lurking around Swing, none are usable because they are all tied to specific components, so my custom component which uses a ListSelectionModel has to have a fourth implementation, which again does exactly the same thing!)

      3. ListSelectionModel subclasses would have the option, if they wished, to override the method and provide a more direct or more efficient implementation based on their internal structure.


      P.S. JList has two more similar loops in getSelectedValues() and getSelectedValuesList(). They could also be simplified by having them call getSelectedIndices(). (And perhaps one of the two could be implemented using the other, if that wouldn't cause problems for subclasses?) This extra change isn't necessary -- it just seems inelegant to have so many similar loops around.


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pbansal Pankaj Bansal
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: