-2

I am trying to call a swing panel from a jar using reflection this is the code

public class coolUI extends JPanel{

public coolUI{

   //swing code here ( made by windowbuilder )

 }
}

Here is the code that i use to call it from

String className = "plugins.plugin1.coolUI";
Class UI = cl.loadClass(className);         
Method theUI = UI.getMethod("coolUI");
Object a = UI.newInstance();
theUI.invoke(a, null);

What i am trying to do here is to load a jar, and to attach the UI inside the jar into a tabbedpane, though i can do this without reflection, i have no idea on how to do this with it.

J. Coco
  • 3
  • 3

2 Answers2

0

coolUI.coolUI() is a constructor and therefore Class.getMethod("coolUI") throws a NoSuchMethodException.

To make it a method a return type must be added

public void coolUI() {
    //swing code here ( made by windowbuilder )
}
Izruo
  • 2,246
  • 1
  • 11
  • 23
  • if I add a return type the UI becomes blank (it doesn't display stuff that i put in with windowbuilder, also viewed from windowbuilder itself it's blank too). – J. Coco Nov 08 '17 at 20:45
  • Well, it seems, that this fixed the `NoSuchMethodException`. Can you somehow determine whether the method is actually called? - e.g. putting in a debug statement - If so, your current error is most likely unrelated to this question. – Izruo Nov 08 '17 at 20:49
  • Yes, the method is indeed called. "If so, your current error is most likely unrelated to this question." what should i do now? can't post anymore questions, should i give free rep/delete or thats breaking the rules? – J. Coco Nov 08 '17 at 20:53
  • I'm sorry, but I'm not too familiar with this. However, you can always look at the [help center](https://stackoverflow.com/help) or at meta, for example [here](https://meta.stackexchange.com/questions/86997/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th). – Izruo Nov 08 '17 at 21:00
0

I think that you are doing this dynamically, am I right? If so in you should use .getConstructor() instead of getMethod().

Like this:

cons = class.getConstructor();
IndieDev
  • 175
  • 1
  • 1
  • 12