2412 lines · 86.9 KB · Java
RawDownload
        if ( editor != null ) {            editor.removeActionListener(this);        }        editor = anEditor;        if ( editor != null ) {            editor.addActionListener(this);        }        firePropertyChange( "editor", oldEditor, editor );    }     /**     * Returns the editor used to paint and edit the selected item in the     * <code>JComboBox</code> field.     *     * @return the <code>ComboBoxEditor</code> that displays the selected item     */    public ComboBoxEditor getEditor() {        return editor;    }     //    // Selection    //     /**     * Sets the selected item in the combo box display area to the object in     * the argument.     * If <code>anObject</code> is in the list, the display area shows     * <code>anObject</code> selected.     * <p>     * If <code>anObject</code> is <i>not</i> in the list and the combo box is     * uneditable, it will not change the current selection. For editable     * combo boxes, the selection will change to <code>anObject</code>.     * <p>     * If this constitutes a change in the selected item,     * <code>ItemListener</code>s added to the combo box will be notified with     * one or two <code>ItemEvent</code>s.     * If there is a current selected item, an <code>ItemEvent</code> will be     * fired and the state change will be <code>ItemEvent.DESELECTED</code>.     * If <code>anObject</code> is in the list and is not currently selected     * then an <code>ItemEvent</code> will be fired and the state change will     * 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     * @beaninfo     *    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.                boolean found = false;                for (int i = 0; i < dataModel.getSize(); i++) {                    E element = dataModel.getElementAt(i);                    if (anObject.equals(element)) {                        found = true;                        objectToSelect = element;                        break;                    }                }                if (!found) {                    return;                }            }             // Must toggle the state of this flag since this method            // call may result in ListDataEvents being fired.            selectingItem = true;            dataModel.setSelectedItem(objectToSelect);            selectingItem = false;             if (selectedItemReminder != dataModel.getSelectedItem()) {                // in case a users implementation of ComboBoxModel                // doesn't fire a ListDataEvent when the selection                // changes.                selectedItemChanged();            }        }        fireActionEvent();    }     /**     * Returns the current selected item.     * <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>.     *     * @param anIndex an integer specifying the list item to select,     *                  where 0 specifies the first item in the list and -1 indicates no selection     * @exception IllegalArgumentException if <code>anIndex</code> < -1 or     *                  <code>anIndex</code> is greater than or equal to size     * @beaninfo     *   preferred: true     *  description: The item at index is selected.     */    public void setSelectedIndex(int anIndex) {        int size = dataModel.getSize();         if ( anIndex == -1 ) {            setSelectedItem( null );        } else if ( anIndex < -1 || anIndex >= size ) {            throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds");        } else {            setSelectedItem(dataModel.getElementAt(anIndex));        }    }     /**     * Returns the first item in the list that matches the given item.     * The result is not always defined if the <code>JComboBox</code>     * allows selected items that are not in the list.     * Returns -1 if there is no selected item or if the user specified     * an item which is not in the list.      * @return an integer specifying the currently selected list item,     *                  where 0 specifies     *                  the first item in the list;     *                  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;    }     /**     * Returns the "prototypical display" value - an Object used     * for the calculation of the display height and width.     *     * @return the value of the <code>prototypeDisplayValue</code> property     * @see #setPrototypeDisplayValue     * @since 1.4     */    public E getPrototypeDisplayValue() {        return prototypeDisplayValue;    }     /**     * Sets the prototype display value used to calculate the size of the display     * for the UI portion.     * <p>     * If a prototype display value is specified, the preferred size of     * the combo box is calculated by configuring the renderer with the     * prototype display value and obtaining its preferred size. Specifying     * the preferred display value is often useful when the combo box will be     * displaying large amounts of data. If no prototype display value has     * been specified, the renderer must be configured for each value from     * the model and its preferred size obtained, which can be     * relatively expensive.     *     * @param prototypeDisplayValue     * @see #getPrototypeDisplayValue     * @since 1.4     * @beaninfo     *       bound: true     *   attribute: visualUpdate true     * description: The display prototype value, used to compute display width and height.     */    public void setPrototypeDisplayValue(E prototypeDisplayValue) {        Object oldValue = this.prototypeDisplayValue;        this.prototypeDisplayValue = prototypeDisplayValue;        firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue);    }     /**     * Adds an item to the item list.     * This method works only if the <code>JComboBox</code> uses a     * mutable data model.     * <p>     * <strong>Warning:</strong>     * Focus and keyboard navigation problems may arise if you add duplicate     * String objects. A workaround is to add new objects instead of String     * objects and make sure that the toString() method is defined.     * For example:     * <pre>     *   comboBox.addItem(makeObj("Item 1"));     *   comboBox.addItem(makeObj("Item 1"));     *   ...     *   private Object makeObj(final String item)  {     *     return new Object() { public String toString() { return item; } };     *   }     * </pre>     *     * @param item the item to add to the list     * @see MutableComboBoxModel     */    public void addItem(E item) {        checkMutableComboBoxModel();        ((MutableComboBoxModel<E>)dataModel).addElement(item);    }     /**     * Inserts an item into the item list at a given index.     * This method works only if the <code>JComboBox</code> uses a     * mutable data model.     *     * @param item the item to add to the list     * @param index    an integer specifying the position at which     *                  to add the item     * @see MutableComboBoxModel     */    public void insertItemAt(E item, int index) {        checkMutableComboBoxModel();        ((MutableComboBoxModel<E>)dataModel).insertElementAt(item,index);    }     /**     * 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     * mutable data model.     *     * @param anIndex  an int specifying the index of the item to remove,     *                  where 0     *                  indicates the first item in the list     * @see MutableComboBoxModel     */    public void removeItemAt(int anIndex) {        checkMutableComboBoxModel();        ((MutableComboBoxModel<E>)dataModel).removeElementAt( anIndex );    }     /**     * Removes all items from the item list.     */    public void removeAllItems() {        checkMutableComboBoxModel();        MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel;        int size = model.getSize();         if ( model instanceof DefaultComboBoxModel ) {            ((DefaultComboBoxModel)model).removeAllElements();        }        else {            for ( int i = 0; i < size; ++i ) {                E element = model.getElementAt( 0 );                model.removeElement( element );            }        }        selectedItemReminder = null;        if (isEditable()) {            editor.setItem(null);        }    }     /**     * Checks that the <code>dataModel</code> is an instance of     * <code>MutableComboBoxModel</code>.  If not, it throws an exception.     * @exception RuntimeException if <code>dataModel</code> is not an     *          instance of <code>MutableComboBoxModel</code>.     */    void checkMutableComboBoxModel() {        if ( !(dataModel instanceof MutableComboBoxModel) )            throw new RuntimeException("Cannot use this method with a non-Mutable data model.");    }     /**     * Causes the combo box to display its popup window.     * @see #setPopupVisible     */    public void showPopup() {        setPopupVisible(true);    }     /**     * Causes the combo box to close its popup window.     * @see #setPopupVisible     */    public void hidePopup() {        setPopupVisible(false);    }     /**     * Sets the visibility of the popup.     */    public void setPopupVisible(boolean v) {        getUI().setPopupVisible(this, v);    }     /**     * Determines the visibility of the popup.     *     * @return true if the popup is visible, otherwise returns false     */    public boolean isPopupVisible() {        return getUI().isPopupVisible(this);    }     /** Selection **/     /**     * Adds an <code>ItemListener</code>.     * <p>     * <code>aListener</code> will receive one or two <code>ItemEvent</code>s when     * the selected item changes.     *     * @param aListener the <code>ItemListener</code> that is to be notified     * @see #setSelectedItem     */    public void addItemListener(ItemListener aListener) {        listenerList.add(ItemListener.class,aListener);    }     /** Removes an <code>ItemListener</code>.     *     * @param aListener  the <code>ItemListener</code> to remove     */    public void removeItemListener(ItemListener aListener) {        listenerList.remove(ItemListener.class,aListener);    }     /**     * Returns an array of all the <code>ItemListener</code>s added     * to this JComboBox with addItemListener().     *     * @return all of the <code>ItemListener</code>s added or an empty     *         array if no listeners have been added     * @since 1.4     */    public ItemListener[] getItemListeners() {        return listenerList.getListeners(ItemListener.class);    }     /**     * Adds an <code>ActionListener</code>.     * <p>     * The <code>ActionListener</code> will receive an <code>ActionEvent</code>     * when a selection has been made. If the combo box is editable, then     * an <code>ActionEvent</code> will be fired when editing has stopped.     *     * @param l  the <code>ActionListener</code> that is to be notified     * @see #setSelectedItem     */    public void addActionListener(ActionListener l) {        listenerList.add(ActionListener.class,l);    }     /** Removes an <code>ActionListener</code>.     *     * @param l  the <code>ActionListener</code> to remove     */    public void removeActionListener(ActionListener l) {        if ((l != null) && (getAction() == l)) {            setAction(null);        } else {            listenerList.remove(ActionListener.class, l);        }    }     /**     * Returns an array of all the <code>ActionListener</code>s added     * to this JComboBox with addActionListener().     *     * @return all of the <code>ActionListener</code>s added or an empty     *         array if no listeners have been added     * @since 1.4     */    public ActionListener[] getActionListeners() {        return listenerList.getListeners(ActionListener.class);    }     /**     * Adds a <code>PopupMenu</code> listener which will listen to notification     * messages from the popup portion of the combo box.     * <p>     * For all standard look and feels shipped with Java, the popup list     * portion of combo box is implemented as a <code>JPopupMenu</code>.     * A custom look and feel may not implement it this way and will     * therefore not receive the notification.     *     * @param l  the <code>PopupMenuListener</code> to add     * @since 1.4     */    public void addPopupMenuListener(PopupMenuListener l) {        listenerList.add(PopupMenuListener.class,l);    }     /**     * Removes a <code>PopupMenuListener</code>.     *     * @param l  the <code>PopupMenuListener</code> to remove     * @see #addPopupMenuListener     * @since 1.4     */    public void removePopupMenuListener(PopupMenuListener l) {        listenerList.remove(PopupMenuListener.class,l);    }     /**     * Returns an array of all the <code>PopupMenuListener</code>s added     * to this JComboBox with addPopupMenuListener().     *     * @return all of the <code>PopupMenuListener</code>s added or an empty     *         array if no listeners have been added     * @since 1.4     */    public PopupMenuListener[] getPopupMenuListeners() {        return listenerList.getListeners(PopupMenuListener.class);    }     /**     * Notifies <code>PopupMenuListener</code>s that the popup portion of the     * combo box will become visible.     * <p>     * This method is public but should not be called by anything other than     * the UI delegate.     * @see #addPopupMenuListener     * @since 1.4     */    public void firePopupMenuWillBecomeVisible() {        Object[] listeners = listenerList.getListenerList();        PopupMenuEvent e=null;        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==PopupMenuListener.class) {                if (e == null)                    e = new PopupMenuEvent(this);                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);            }        }    }     /**     * Notifies <code>PopupMenuListener</code>s that the popup portion of the     * combo box has become invisible.     * <p>     * This method is public but should not be called by anything other than     * the UI delegate.     * @see #addPopupMenuListener     * @since 1.4     */    public void firePopupMenuWillBecomeInvisible() {        Object[] listeners = listenerList.getListenerList();        PopupMenuEvent e=null;        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==PopupMenuListener.class) {                if (e == null)                    e = new PopupMenuEvent(this);                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);            }        }    }     /**     * Notifies <code>PopupMenuListener</code>s that the popup portion of the     * combo box has been canceled.     * <p>     * This method is public but should not be called by anything other than     * the UI delegate.     * @see #addPopupMenuListener     * @since 1.4     */    public void firePopupMenuCanceled() {        Object[] listeners = listenerList.getListenerList();        PopupMenuEvent e=null;        for (int i = listeners.length-2; i>=0; i-=2) {            if (listeners[i]==PopupMenuListener.class) {                if (e == null)                    e = new PopupMenuEvent(this);                ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);

Lines 501–1000 of 2412. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t