Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way to pass a function as a string

Tags:

java

Please allow me to explain what I am trying to do - I think that the title explains it roughly, but I am none too sure that I am going about things the right way, so please correct me if I am wrong!

I have created a custom dialog using a LayeredPane. Essentially a jPanel is shown on the POPUP_LAYER, thus appearing over the top of the main content. This panel contains a simple 'label-textbox-okay-cancel' group of controls. I will call this the 'Dialog Panel'.

What am I trying to do: When a button is clicked on the main window (contained within the LayeredPane), the Dialog Panel will appear and allow the user to enter some text, then click okay or cancel. This is all easy enough, but I would like this code to be re-usable, which means that other buttons on the main window will invoke the same Dialog Panel, but with different text in the label.

This of course requires me to include some kind of callback function so that the okay button on the Dialog Panel will run the correct code, according to the button which invoked it.

My current attempt is to store a string which will contain the name of the function that should be run when the user clicks the okay button on the Dialog Panel. I am attempting to retrieve this string and convert it into the function name and so far I have found many references to 'Reflection', many of them suggesting that it is not a good idea.

In any case I have been unable to get any example code to work because I do not understand what the 'obj' is in the code below and am unable to invoke the method:

method = obj.getClass().getMethod(myCallbackString);

My questions are:

Firstly, am I even going about this the right way? I am more than open to suggestions, but please try to keep it as simple as possible because I really am just getting started!

Secondly, what is the 'obj' in the code shown above? I would really like to know, even if this is not the way that I should be doing things!

My other thoughts include: Should my Dialog Panel be in its own class, and if so, again, how to pass the callback function?

Any help or advice would be gratefully received.

BTW: In answer to the question "why not use a normal dialog?" I can only say that I am currently experimenting, and I simply want to see if I can do this!

MVK

like image 256
MaxVK Avatar asked Dec 15 '22 22:12

MaxVK


2 Answers

The usual way the callback functions are passed in by Java programs is by passing instances of interfaces that implement a specific callback function. It is typical, though not required, to implement the interface anonymously.

For example, here is an interface:

interface MyCallback {
    void performCallback();
}

Here is the way you define your dialog's method that takes a callback:

void openWithCallback(MyCallback cb) {
    // Do something useful...
    ...
    // Perform the callback
    cb.performCallback();
}

Here is the way that you invoke that method:

public void OpenDialog() {
    myDialog.openWithCallback(new MyCallback() {
        public void performCallback() {
            System.out.println("Inside callback...");
        }
    });
}
like image 104
Sergey Kalinichenko Avatar answered Dec 28 '22 03:12

Sergey Kalinichenko


obj names the object whose method you want to call, in your case you can probably replace it with this (or drop it out entirely, since this is implied):

private void callByName(String name) { 
  try { getClass().getMethod(name).invoke(this); }
  catch (RuntimeException e) { throw e; }
  catch (Exception e) { throw new RuntimeException(e); }
}

For this to work you need to declare a public no-arg method with the appropriate name.

like image 31
Marko Topolnik Avatar answered Dec 28 '22 02:12

Marko Topolnik