Question

I am building a user interface in netBeans (coding by hand, more flexible) with multiple toolbars.

What I am trying to do is create an actionListener for each button. I am retrieving names of the functions from XML and parse them to string. I will write implementations for those functions in a separate class, but my problem is the following:

How do I make the link between the function name and the string containing it's name?

Example: String is Open(), function will be Open(someParameter) and in the definitions class there will be static void Open(param).

Was it helpful?

Solution

First of all, consider my comment about your idea of dynamic button behavior resolved from strings being a wrong approach. However if you still need exactly what you asked, what you need is Reflection API.

Here's an example:

    Class c = SomeClassWithMethods.class;
    Method m = c.getMethod("someMethodName", String.class, Integer.class, Integer.TYPE);
    m.invoke(baseObjectFromWhichToCallTheMethod, "stringParam", 10, 5);

Added:

Another option, which is a little bit prettier than reflection, but still a messy design, would be to use a map to link those Strings to methods. The code is a bit longer, but from the Java perspective it is much better than using reflection for your task (unless you have some specific requirement of which I'm not aware). This is how it would work:

//Interface whose instances will bind strings to methods
interface ButtonClickHandler {
    void onClick();
}

class SomeClassYouNeed {
    //One of the methods that will be bound to "onButtonOneClick()"
    public void onButtonOneClick() {
        log.info("ButtonOneClick method is called");
    }

    public void onButtonTwoClick() {
        log.info("ButtonTwoClick method is called");
    }

    //Map that will hold your links
    private static Map<String, ButtonClickHandler> buttonActionMap;

    //Static constructor to initialize the map
    static {
        buttonActionMap = new Map<String, ButtonClickHandler>();
        buttonActionMap.put("onButtonOneClick()",new ButtonClickHandler() {
            @Override
            public void onClick() {
                onButtonOneClick();
            }
        });
        buttonActionMap.put("onButtonTwoClick()",new ButtonClickHandler() {
            @Override
            public void onClick() {
                onButtonTwoClick();
            }
        });
    }

    public void callByName(String methodName) {
        final ButtonClickHandler handler = buttonActionMap.get(methodName);
        if (handler == null) {
            throw new IllegalArgumentException("No handler found by name: "+methodName); 
        }
        handler.onClick();
    }
}

After you call callByName("onButtonTwoClick()") it will fetch the respective instance of ButtonClickHandler which will use the static method onButtonTwoClick() to process the click of the button.

OTHER TIPS

It seems to me that you are looking for the equivalent of JS "eval" function in Java. This might help. Nevertheless it is generally not a good idea as @Max stated, you might want to rethink your design.

If i have understood your question correctly you are trying to generate your code files based on some strings taken from a XML file. I can suggest you this library to generate your codes. For tutorials you can visit this link. You may even use the Java Reflection API. Here is a link for the tutorial.

Its upto you, that which of the above two you use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top