Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:01:34+00:00 2026-05-27T13:01:34+00:00

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

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:01:34+00:00Added an answer on May 27, 2026 at 1:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all im building a web user interface. In this interface i use popup
I'm building a simple application. It's a user interface to an online order system.
What are some recommended WordPress plugins that make building an online user manual more
Alright all you wonderful people out there; In my user interface I am building
There are a lot of flexible, complete, cross-platform, et cetera , graphical user interface
I am building an user interface. My program will consist of 4 main parts:
Building my baseclasses for user interface controls is getting there. I have command buttons
I am building a tabbed interface for switching between various similar layers. Each layer
I'm currently building a multiple-document interface application, but I'm having a problem when the
I'm building a tree-based debug/logging system for C++. Its user interface is a macro

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.