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

JList.setSelectedValue(null, ...) doesn't do anything

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 11
    • client-libs
    • None
    • behavioral
    • minimal
    • Hide
      The default behaviour of setSelectedValue will remain same unless the passed object is null. When the object passed is null, then the setSelectedValue will now clear the selection which it was not doing earlier. But as users may also be using getSelectedValue, they must be aware of the meaning of "null" for these methods.
      Show
      The default behaviour of setSelectedValue will remain same unless the passed object is null. When the object passed is null, then the setSelectedValue will now clear the selection which it was not doing earlier. But as users may also be using getSelectedValue, they must be aware of the meaning of "null" for these methods.
    • Java API
    • SE

      Summary

      Change method JList.setSelectedValue to clear selection when the object passed is null.

      Problem

      JList.setSelection method curretly calls JList.setSelectedIndex(-1) when the object passed is null. But calling setSelectedIndex(-1) does not serve any purpose. The expectation is that it should clear the selection when "null" is passed, as the function getSelectedValue returns "null" when nothing is selected. This makes these API's more compatible.

      Solution

      The change is to call clearSelection() instead of setSelectedIndex(-1) when the object passed is "null".

      webrev: http://cr.openjdk.java.net/~pbansal/5076761/webrev.06/

      Specification

      The details of setSelectedValue of JList class which will be changed are as

      src/java.desktop/share/classes/javax/swing/JList.java

          /**
           * Selects the specified object from the list.
           * If the object passed is {@code null}, the selection is cleared.
           *
           * @param anObject      the object to select
           * @param shouldScroll  {@code true} if the list should scroll to display
           *                      the selected object, if one exists; otherwise {@code false}
           */
           public void setSelectedValue(Object anObject,boolean shouldScroll) {}

            pbansal Pankaj Bansal (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Prasanta Sadhukhan, Sergey Bylokhov
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: