Make a jmenuItem. - Java Swing

Java examples for Swing:JMenu

Description

Make a jmenuItem.

Demo Code

/*//from  w w  w . j  a v a  2 s .  c  om
 * $Id: GuiUtils.java,v 1.317 2007/08/10 14:26:33 jeffmc Exp $
 *
 * Copyright  1997-2015 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
//package com.java2s;

import java.awt.event.*;

import java.lang.reflect.*;
import java.util.ArrayList;

import javax.swing.*;

public class Main {
    /**
     * Make a jmenuItem. Call methodName on object when menuItem pressed. Pass in given arg
     * if non-null.
     *
     * @param label Label
     * @param object Object to call
     * @param methodName Method name to call
     * @param arg Pass this to method name if non-null.
     *
     * @return The menuItem
     */
    public static JMenuItem makeMenuItem(String label, final Object object,
            final String methodName, final Object arg) {
        return makeMenuItem(label, object, methodName, arg, false);
    }

    /**
     * Make a jmenuItem. Call methodName on object when menuItem pressed.
     *
     * @param label Label
     * @param object Object to call
     * @param methodName Method name to call
     *
     * @return The menuItem
     */
    public static JMenuItem makeMenuItem(String label, final Object object,
            final String methodName) {
        return makeMenuItem(label, object, methodName, null, false);
    }

    /**
     * Make a jmenuItem. Call methodName on object when menuItem pressed. Pass in given arg
     * if non-null.
     *
     * @param label Label
     * @param object Object to call
     * @param methodName Method name to call
     * @param arg Pass this to method name if non-null.
     * @param inThread If true then call the method in a thread
     *
     * @return The menuItem
     */
    public static JMenuItem makeMenuItem(String label, final Object object,
            final String methodName, final Object arg,
            final boolean inThread) {
        final Method theMethod = findMethod(object, methodName, arg);
        final JMenuItem mi = new JMenuItem(label);
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (inThread) {
                    Thread t = new Thread() {
                        public void run() {
                            invokeMethod();
                        }
                    };
                    t.start();
                } else {
                    invokeMethod();
                }
            }

            public void invokeMethod() {
                try {
                    if (arg == null) {
                        theMethod.invoke(object, new Object[] {});
                    } else {
                        theMethod.invoke(object, new Object[] { arg });
                    }
                } catch (Exception exc) {
                    //                    LogUtil.logException("Error in makeMenuItem", exc);
                }
            }
        };
        mi.addActionListener(listener);
        return mi;
    }

    /**
     * Find all methods with the given name.
     * Of these methods find one whose parameter types
     * are assignable from the given parameter types.
     *
     * @param c            class to check
     * @param methodName   name of method
     * @param paramTypes   parameter types
     * @return  class method or <code>null</code> if one doesn't exist
     */
    public static Method findMethod(Class c, String methodName,
            Class[] paramTypes) {
        ArrayList all = new ArrayList();
        Method[] methods = c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (!methodName.equals(methods[i].getName())) {
                continue;
            }
            if (paramTypes == null) {
                return methods[i];
            }
            if (typesMatch(methods[i].getParameterTypes(), paramTypes)) {
                all.add(methods[i]);
            }
        }
        if (all.size() > 1) {
            String msg = "More than one method: " + methodName
                    + " found for class:" + c.getName();
            for (int i = 0; i < paramTypes.length; i++) {
                if (paramTypes[i] != null) {
                    msg += " " + paramTypes[i].getName();
                }
            }
            throw new IllegalArgumentException(msg);
        }
        if (all.size() == 1) {
            return (Method) all.get(0);
        }
        return null;
    }

    /**
     * Find a method
     *
     * @param object     Object with the method in it
     * @param methodName method name
     * @param arg        method argument
     *
     * @return  the method
     */
    private static Method findMethod(Object object, String methodName,
            Object arg) {

        Method theMethod = null;
        if (arg == null) {
            theMethod = findMethod(object.getClass(), methodName,
                    new Class[] {});
        } else {
            theMethod = findMethod(object.getClass(), methodName,
                    new Class[] { arg.getClass() });
        }
        if (theMethod == null) {
            System.err.println("arg = " + arg);
            throw new IllegalArgumentException("Unknown method:"
                    + object.getClass() + "." + methodName + "("
                    + ((arg == null) ? "" : arg.getClass().getName()) + ")");
        }
        return theMethod;

    }

    /**
     * Returns true if the Classes defined in the actual parameter
     * are equal or a sub-class of the corresponding classes defined in the
     * formal argument.
     *
     * @param formals     formal classes (types)
     * @param actuals     actual classes
     * @return   true  if they match
     */
    public static boolean typesMatch(Class[] formals, Class[] actuals) {
        if (formals.length != actuals.length) {
            return false;
        }
        for (int j = 0; j < formals.length; j++) {
            if (actuals[j] == null) {
                continue;
            }
            if (!formals[j].isAssignableFrom(actuals[j])) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials