e650. 激活事件

An object wishing to fire item events must implement ItemSelectable. This example shows typical code that an object must implement to fire item events. When an item event is to be fired, fireItemEvent() should be called.

    public class MyComponent implements ItemSelectable {
        protected EventListenerList listenerList = new EventListenerList();
    
        public Object[] getSelectedObjects() {
            return selectedObjects;
        }
    
        public void addItemListener(ItemListener l) {
            listenerList.add(ItemListener.class, l);
        }
    
        public void removeItemListener(ItemListener l) {
            listenerList.remove(ItemListener.class, l);
        }
    
        // If the item is selected, sel should be true.
        void fireItemEvent(Object item, boolean sel) {
            ItemEvent evt = new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                item, sel ? ItemEvent.SELECTED : ItemEvent.DESELECTED);
    
            // Get list of listeners
            Object[] listeners = listenerList.getListenerList();
    
            // Send event to all listeners
            for (int i=0; i<listeners.length-2; i+=2) {
                if (listeners[i] == ItemListener.class) {
                    ((ItemListener)listeners[i+1]).itemStateChanged(evt);
                }
            }
        }
    }

 

Related Examples
posted @ 2018-09-02 21:24  borter  阅读(129)  评论(0编辑  收藏  举报