public class ReflectiveItemListener extends Object implements ItemListener
Normally there are two standard ways to implement the ItemListener interface: The first is to let a class implement the ItemListener interface and then add an object of this class to the addItemListener methods of the components fireing ItemEvents. You then have to check the source in the itemStateChanged method. You end up with a big ugly, unmaintainable and unstable if-then-else- statement. The second approach is to define a class for every such component. You end up with tons of small classes all increasing the memory footprint (typically 3K per class). This approach using reflection lets you define a method for each such component avoiding the mentioned drawbacks. The new drawbacks are however:
Note: This class might change package in a future version!
Note: This class might throw different Exceptions in a future version!
Constructor and Description |
---|
ReflectiveItemListener(Object target,
String methodName)
Creates a new instance of this class.
|
Modifier and Type | Method and Description |
---|---|
void |
itemStateChanged(ItemEvent e)
Invoked when an item has been selected or deselected by the user.
|
public ReflectiveItemListener(Object target, String methodName) throws NoSuchMethodException
target
- the object with the specified methodmethodName
- the method name (must take an ItemEvent
obect as its single parameter)NoSuchMethodException
- if no such method foundpublic void itemStateChanged(ItemEvent e)
itemStateChanged
in interface ItemListener
e
- an ItemEvent objectCopyright © 2002–2016 SoftSmithy. All rights reserved.