< prev index next >

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

Print this page

    rev 48858 : 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.
    3. Reverted the changes made to "getSelectedValues" as suggested by Joe.
    4. Modified files that were failing on Linux and Mac as well.

    @@ -38,11 +38,11 @@
      * @since 1.2
      */
     @SuppressWarnings("serial") // Superclass is not serializable across versions
     public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {
         Vector<E> objects;
    -    Object selectedObject;
    +    E selectedObject;

         /**
          * Constructs an empty DefaultComboBoxModel object.
          */
         public DefaultComboBoxModel() {

    @@ -82,23 +82,25 @@
         }

         // implements javax.swing.ComboBoxModel
         /**
          * Set the value of the selected item. The selected item may be null.
    -     *
    +     * @param <E> the type of the elements of this model.
          * @param anObject The combo box value or null for no selection.
          */
    -    public void setSelectedItem(Object anObject) {
    +    @Override
    +    public void setSelectedItem(E anObject) {
             if ((selectedObject != null && !selectedObject.equals( anObject )) ||
                 selectedObject == null && anObject != null) {
                 selectedObject = anObject;
                 fireContentsChanged(this, -1, -1);
             }
         }

         // implements javax.swing.ComboBoxModel
    -    public Object getSelectedItem() {
    +    @Override
    +    public E getSelectedItem() {
             return selectedObject;
         }

         // implements javax.swing.ListModel
         public int getSize() {

    @@ -114,15 +116,16 @@
         }

         /**
          * Returns the index-position of the specified object in the list.
          *
    +     * @param <E> the type of the elements of this model.
          * @param anObject the object to return the index of
          * @return an int representing the index position, where 0 is
          *         the first position
          */
    -    public int getIndexOf(Object anObject) {
    +    public int getIndexOf(E anObject) {
             return objects.indexOf(anObject);
         }

         // implements javax.swing.MutableComboBoxModel
         public void addElement(E anObject) {

    @@ -154,11 +157,11 @@

             fireIntervalRemoved(this, index, index);
         }

         // implements javax.swing.MutableComboBoxModel
    -    public void removeElement(Object anObject) {
    +    public void removeElement(E anObject) {
             int index = objects.indexOf(anObject);
             if ( index != -1 ) {
                 removeElementAt(index);
             }
         }

< prev index next >