001/** 002* Please feel free to use any fragment of the code in this file that you need 003* in your own work. As far as I am concerned, it's in the public domain. No 004* permission is necessary or required. Credit is always appreciated if you 005* use a large chunk or base a significant product on one of my examples, 006* but that's not required either. 007* 008* This code is distributed in the hope that it will be useful, 009* but WITHOUT ANY WARRANTY; without even the implied warranty of 010* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 011* 012* --- Joseph A. Huwaldt 013**/ 014package jahuwaldt.swing; 015 016import java.awt.event .*; 017import java.lang.reflect .*; 018 019/** 020* A class that implements a generic action listener. 021* This listener can be dynamically instantiated again 022* and again any time where you need an action listener. 023* 024* <p> Modified by: Joseph A. Huwaldt </p> 025* 026* @author Joseph A. Huwaldt Date: February 16, 2000 027* @version September 14, 2012 028**/ 029public class GenericActionListener implements ActionListener { 030 031 private final Object target; 032 033 private final Method targetMethod; 034 035 036 public GenericActionListener( Object target, Method targetMethod ) { 037 super(); 038 039 this.target = target; 040 this.targetMethod = targetMethod; 041 } 042 043 @Override 044 public void actionPerformed( ActionEvent event ) { 045 try { 046 047 targetMethod.invoke( target, new Object []{ event } ); 048 049 } catch( IllegalAccessException e ) { 050 e.printStackTrace(); 051 } catch( InvocationTargetException e ) { 052 e.printStackTrace(); 053 } 054 } 055 056 057} 058 059