- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- public class ActionHelper {
- public static EventHandler<ActionEvent> handle(Object obj,String methodName,Object... param){
- return new Action(obj,methodName,param);
- }
- private static class Action implements EventHandler<ActionEvent>{
- private Object obj;
- private String methodName;
- private Class<?>[] clazz;
- private Object[] param;
- public Action(Object obj,String methodName,Object... param){
- this.obj = obj;
- if(param.length>0){
- clazz = new Class[1+param.length];
- clazz[0] = ActionEvent.class;
- for(int i=0;i<param.length;i++){
- clazz[1+i] = param[i].getClass();
- }
- this.param = new Object[1+param.length];
- for(int i=0;i<param.length;i++){
- this.param[1+i] = param[i];
- }
- }else{
- clazz = new Class[1];
- clazz[0] = ActionEvent.class;
- this.param = new Object[1];
- }
- this.methodName = methodName;
- }
- @Override
- public void handle(ActionEvent event) {
- try {
- Method method = obj.getClass().getDeclaredMethod(methodName, clazz);
- if(method != null){
- method.setAccessible(true);
- param[0] = event;
- method.invoke(obj, param);
- }
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
- }
- //以下为使用的代码片段,javafx的右键菜单
- ContextMenu ctxMenu = new ContextMenu();
- MenuItem mitem = new MenuItem("查看页面");
- mitem.setOnAction(ActionHelper.handle(this, "viewPage"));
- ctxMenu.getItems().add(mitem);
- private void viewPage(ActionEvent event){
- //do some thing
- }
- //该片段来自于http://www.codesnippet.cn/detail/130920135853.html
来源: http://www.codesnippet.cn/detail/130920135853.html