The correct way to pass a function as a string
<p>Please allow me to explain what I am trying to do - I <em>think</em> 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!</p>

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

<p>What am I trying to do:
When a button is clicked on the main window (contained within the <code>LayeredPane</code>), the <em>Dialog Panel</em> 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 <em>Dialog Panel</em>, but with different text in the label.</p>

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

<p>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 <em>Dialog Panel</em>. 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.</p>

<p>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:</p>

<pre><code>method = obj.getClass().getMethod(myCallbackString);
</code></pre>

<p>My questions are:</p>

<p>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!</p>

<p>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!</p>

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

<p>Any help or advice would be gratefully received.</p>

<p>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!</p>

<p>MVK</p>
