< 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,11 +156,11 @@

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

         private E prototypeDisplayValue;

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

    @@ -542,18 +542,19 @@
          * 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(Object anObject) {
    -        Object oldSelection = selectedItemReminder;
    -        Object objectToSelect = anObject;
    +    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,14 +595,15 @@
          * <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 Object getSelectedItem() {
    +    public E getSelectedItem() {
             return dataModel.getSelectedItem();
         }

         /**
          * Selects the item at index <code>anIndex</code>.

    @@ -638,16 +640,14 @@
          *                  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;
    +        E sObject = dataModel.getSelectedItem();

    -        for ( i=0,c=dataModel.getSize();i<c;i++ ) {
    -            obj = dataModel.getElementAt(i);
    +        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,16 +734,17 @@
         /**
          * 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(Object anObject) {
    +    public void removeItem(E anObject) {
             checkMutableComboBoxModel();
    -        ((MutableComboBoxModel)dataModel).removeElement(anObject);
    +        ((MutableComboBoxModel<E>)dataModel).removeElement(anObject);
         }

         /**
          * Removes the item at <code>anIndex</code>
          * This method works only if the <code>JComboBox</code> uses a

    @@ -1294,32 +1295,38 @@
         /**
          * 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)
    -    public Object[] getSelectedObjects() {
    -        Object selectedObject = getSelectedItem();
    -        if ( selectedObject == null )
    -            return new Object[0];
    +    @SuppressWarnings("unchecked")
    +    public E[] getSelectedObjects() {
    +        E selectedObject = getSelectedItem();
    +        E[] result = null;
    +        if ( selectedObject == null ){
    +            result = (E[]) new Object[0];
    +        }
             else {
    -            Object result[] = new Object[1];
    +            result = (E[]) new Object[1];
                 result[0] = selectedObject;
    -            return result;
             }
    +
    +        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(getEditor().getItem());
    +        getModel().setSelectedItem((E)getEditor().getItem());
             String oldCommand = getActionCommand();
             setActionCommand("comboBoxEdited");
             fireActionEvent();
             setActionCommand(oldCommand);
         }

    @@ -1327,12 +1334,12 @@
         /**
          * 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();
    +        E oldSelection = selectedItemReminder;
    +        E newSelection = dataModel.getSelectedItem();
             if (oldSelection == null || !oldSelection.equals(newSelection)) {
                 selectedItemChanged();
                 if (!selectingItem) {
                     fireActionEvent();
                 }

    @@ -1942,11 +1949,11 @@
              *
              * @return the number of items currently selected.
              * @since 1.3
              */
             public int getAccessibleSelectionCount() {
    -            Object o = JComboBox.this.getSelectedItem();
    +            E o = JComboBox.this.getSelectedItem();
                 if (o != null) {
                     return 1;
                 } else {
                     return 0;
                 }

< prev index next >