1

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!

1
  • please learn java naming conventions and stick to them Commented Feb 19, 2012 at 14:18

4 Answers 4

2

If all you need is for the class to extend JPanel so that you can add it to the Tab, try the following.

Class<? extends JPanel> c =  Class.forName(myClassesList[i]).asSubclass(JPanel.class);
JPanel panel = c.newInstance();
jTabbedPane1.addTab(panel);

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

public abstract class ATest extends JPanel{
    public abstract String doSomething();
}

Then you have to implement your concrete classes

public class Class1 extends ATest{
    public Class1(){
        //init me
    }

    @Override
    public String doSomething() {
        return "Class 2";
    }
}

Now you will be free to initialize the class and do what you please using reflection

Class<? extends ATest> c =  Class.forName(myClassesList[i]).asSubclass(ATest.class);
ATest panel = c.newInstance();

System.out.println(panel.doSomething());
jTabbedPane1.addTab(panel);
Sign up to request clarification or add additional context in comments.

Comments

1

You'll probably want to do something like:

c[i].getConstructor(....).newInstance(....);

Where .... contain the relevant constructor parameters.

1 Comment

I can't add c[i].getConstructor(....).newInstance(....); to jTabbedPane1.addTab("myPanelName", <<<here>>);
0

You want to use Java reflection.

Comments

0

You cannot invoke new on a class object, only on the class itself, by name, as it appears in the code which defines it. If you don't have access to the class in this way, then you have to use reflection.

For reference:

class MyClass ...

can be instantiated with new, but:

MyClass.class

is the object representing that class, and cannot be instantiated with new. In your case, this:

MyClass.class.newInstance();

should suffice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.