I have specific classes that i need to load. There name are stored in a String array. I can load the specific classes, but i can’t manipulate proprely.
String [] myClassesList = {"Class1", "Class2", "Class3"...};
FileClassLoader loader = new FileClassLoader("MyClassesPath");
Class [] c = new java.lang.Class[myClassesList.length];
for(int i=0; i<myClassesList.length; i++){
c[i]=loader.loadClass (myClassesList[i]);
... //This work proprely
}
but i can’t
c[i] myNewCi = new c[i].<constructor>();
Note: All my classes are extended JPanel. I need to add to a jTabbedPane. I’m trying to do the following code.
...
myPanelClass myPanel = new myPanelClass();
jTabbedPane1.addTab("myPanelName", myPanel);
... //This work proprely
Any feedback would help. Thanks!
If all you need is for the class to extend JPanel so that you can add it to the Tab, try the following.
This will create a new class (using Reflection) which extends JPanel, but you will be limited in its functionality unless you typecast it.
If you decide you need to interact more with your Classes (call other methods etc) and don’t want to typecast, You will have to define a contract for the classes. This would mean adding an abstract class between your classes and have the abstract class extending JPanel. Here is an example.
Define the Abstract class. this will have any extra method names you want in your classes
Then you have to implement your concrete classes
Now you will be free to initialize the class and do what you please using reflection