Firing Item Events

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); } } } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.