< prev index next >

## src/java.desktop/share/classes/javax/swing/JComboBox.java

Print this page

    rev 48358 : Work in progress for JDK-6303622
    Use generic in Swing components like JComboBox
    1. Added generic return type and parameter type for JComboBox.
    2. Modified JList related classes as well.

    *** 156,166 ****

          /**
           * This protected field is implementation specific. Do not access directly
           * or override.
           */
    !     protected Object selectedItemReminder = null;

          private E prototypeDisplayValue;

          // Flag to ensure that infinite loops do not occur with ActionEvents.
          private boolean firingActionEvent = false;
    --- 156,166 ----

          /**
           * This protected field is implementation specific. Do not access directly
           * or override.
           */
    !     protected E selectedItemReminder = null;

          private E prototypeDisplayValue;

          // Flag to ensure that infinite loops do not occur with ActionEvents.
          private boolean firingActionEvent = false;
    *** 542,559 ****
           * be <code>ItemEvent.SELECTED</code>.
           * <p>
           * <code>ActionListener</code>s added to the combo box will be notified
           * with an <code>ActionEvent</code> when this method is called.
           *
           * @param anObject  the list object to select; use <code>null</code> to
                              clear the selection
           */
          @BeanProperty(bound = false, preferred = true, description
                  = "Sets the selected item in the JComboBox.")
    !     public void setSelectedItem(Object anObject) {
    !         Object oldSelection = selectedItemReminder;
    !         Object objectToSelect = anObject;
              if (oldSelection == null || !oldSelection.equals(anObject)) {

                  if (anObject != null && !isEditable()) {
                      // For non editable combo boxes, an invalid selection
                      // will be rejected.
    --- 542,560 ----
           * be <code>ItemEvent.SELECTED</code>.
           * <p>
           * <code>ActionListener</code>s added to the combo box will be notified
           * with an <code>ActionEvent</code> when this method is called.
           *
    +      * @param <E> the type of the elements of this comboBox.
           * @param anObject  the list object to select; use <code>null</code> to
                              clear the selection
           */
          @BeanProperty(bound = false, preferred = true, description
                  = "Sets the selected item in the JComboBox.")
    !     public void setSelectedItem(E anObject) {
    !         E oldSelection = selectedItemReminder;
    !         E objectToSelect = anObject;
              if (oldSelection == null || !oldSelection.equals(anObject)) {

                  if (anObject != null && !isEditable()) {
                      // For non editable combo boxes, an invalid selection
                      // will be rejected.
    *** 594,607 ****
           * <p>
           * If the combo box is editable, then this value may not have been added
           * to the combo box with <code>addItem</code>, <code>insertItemAt</code>
           * or the data constructors.
           *
           * @return the current selected Object
           * @see #setSelectedItem
           */
    !     public Object getSelectedItem() {
              return dataModel.getSelectedItem();
          }

          /**
           * Selects the item at index <code>anIndex</code>.
    --- 595,609 ----
           * <p>
           * If the combo box is editable, then this value may not have been added
           * to the combo box with <code>addItem</code>, <code>insertItemAt</code>
           * or the data constructors.
           *
    +      * @param <E> the type of the elements of comboBox.
           * @return the current selected Object
           * @see #setSelectedItem
           */
    !     public E getSelectedItem() {
              return dataModel.getSelectedItem();
          }

          /**
           * Selects the item at index <code>anIndex</code>.
    *** 638,653 ****
           *                  or -1 if no item is selected or if
           *                  the currently selected item is not in the list
           */
          @Transient
          public int getSelectedIndex() {
    !         Object sObject = dataModel.getSelectedItem();
    !         int i,c;
    !         E obj;

    !         for ( i=0,c=dataModel.getSize();i<c;i++ ) {
    !             obj = dataModel.getElementAt(i);
                  if ( obj != null && obj.equals(sObject) )
                      return i;
              }
              return -1;
          }
    --- 640,653 ----
           *                  or -1 if no item is selected or if
           *                  the currently selected item is not in the list
           */
          @Transient
          public int getSelectedIndex() {
    !         E sObject = dataModel.getSelectedItem();

    !         for ( int i=0,c=dataModel.getSize();i<c;i++ ) {
    !             E obj = dataModel.getElementAt(i);
                  if ( obj != null && obj.equals(sObject) )
                      return i;
              }
              return -1;
          }
    *** 734,749 ****
          /**
           * Removes an item from the item list.
           * This method works only if the <code>JComboBox</code> uses a
           * mutable data model.
           *
           * @param anObject  the object to remove from the item list
           * @see MutableComboBoxModel
           */
    !     public void removeItem(Object anObject) {
              checkMutableComboBoxModel();
    !         ((MutableComboBoxModel)dataModel).removeElement(anObject);
          }

          /**
           * Removes the item at <code>anIndex</code>
           * This method works only if the <code>JComboBox</code> uses a
    --- 734,750 ----
          /**
           * Removes an item from the item list.
           * This method works only if the <code>JComboBox</code> uses a
           * mutable data model.
           *
    +      * @param <E> the type of the elements of this comboBox.
           * @param anObject  the object to remove from the item list
           * @see MutableComboBoxModel
           */
    !     public void removeItem(E anObject) {
              checkMutableComboBoxModel();
    !         ((MutableComboBoxModel<E>)dataModel).removeElement(anObject);
          }

          /**
           * Removes the item at <code>anIndex</code>
           * This method works only if the <code>JComboBox</code> uses a
    *** 1294,1325 ****
          /**
           * Returns an array containing the selected item.
           * This method is implemented for compatibility with
           * <code>ItemSelectable</code>.
           *
           * @return an array of <code>Objects</code> containing one
           *          element -- the selected item
           */
          @BeanProperty(bound = false)
    !     public Object[] getSelectedObjects() {
    !         Object selectedObject = getSelectedItem();
    !         if ( selectedObject == null )
    !             return new Object[0];
              else {
    !             Object result[] = new Object[1];
                  result[0] = selectedObject;
    -             return result;
              }
          }

          /**
           * This method is public as an implementation side effect.
           * do not call or override.
           */
          public void actionPerformed(ActionEvent e) {
              setPopupVisible(false);
    !         getModel().setSelectedItem(getEditor().getItem());
              String oldCommand = getActionCommand();
              setActionCommand("comboBoxEdited");
              fireActionEvent();
              setActionCommand(oldCommand);
          }
    --- 1295,1332 ----
          /**
           * Returns an array containing the selected item.
           * This method is implemented for compatibility with
           * <code>ItemSelectable</code>.
           *
    +      * @param <E> the type of the elements of this comboBox.
           * @return an array of <code>Objects</code> containing one
           *          element -- the selected item
           */
          @BeanProperty(bound = false)
    !     @SuppressWarnings("unchecked")
    !     public E[] getSelectedObjects() {
    !         E selectedObject = getSelectedItem();
    !         E[] result = null;
    !         if ( selectedObject == null ){
    !             result = (E[]) new Object[0];
    !         }
              else {
    !             result = (E[]) new Object[1];
                  result[0] = selectedObject;
              }
    +
    +         return result;
          }

          /**
           * This method is public as an implementation side effect.
           * do not call or override.
           */
    +     @SuppressWarnings("unchecked")
          public void actionPerformed(ActionEvent e) {
              setPopupVisible(false);
    !         getModel().setSelectedItem((E)getEditor().getItem());
              String oldCommand = getActionCommand();
              setActionCommand("comboBoxEdited");
              fireActionEvent();
              setActionCommand(oldCommand);
          }
    *** 1327,1338 ****
          /**
           * This method is public as an implementation side effect.
           * do not call or override.
           */
          public void contentsChanged(ListDataEvent e) {
    !         Object oldSelection = selectedItemReminder;
    !         Object newSelection = dataModel.getSelectedItem();
              if (oldSelection == null || !oldSelection.equals(newSelection)) {
                  selectedItemChanged();
                  if (!selectingItem) {
                      fireActionEvent();
                  }
    --- 1334,1345 ----
          /**
           * This method is public as an implementation side effect.
           * do not call or override.
           */
          public void contentsChanged(ListDataEvent e) {
    !         E oldSelection = selectedItemReminder;
    !         E newSelection = dataModel.getSelectedItem();
              if (oldSelection == null || !oldSelection.equals(newSelection)) {
                  selectedItemChanged();
                  if (!selectingItem) {
                      fireActionEvent();
                  }
    *** 1942,1952 ****
               *
               * @return the number of items currently selected.
               * @since 1.3
               */
              public int getAccessibleSelectionCount() {
    !             Object o = JComboBox.this.getSelectedItem();
                  if (o != null) {
                      return 1;
                  } else {
                      return 0;
                  }
    --- 1949,1959 ----
               *
               * @return the number of items currently selected.
               * @since 1.3
               */
              public int getAccessibleSelectionCount() {
    !             E o = JComboBox.this.getSelectedItem();
                  if (o != null) {
                      return 1;
                  } else {
                      return 0;
                  }

< prev index next >