Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:58:34+00:00 2026-05-30T03:58:34+00:00

I have specific classes that i need to load. There name are stored in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:58:35+00:00Added an answer on May 30, 2026 at 3:58 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need for methods in several classes that must always follow a
I have read several articles on when to use nested classes, but none that
I have an application which has some classes that handle some specific functions, have
I have some very small classes that I feel should be pulled up but
I have moved my classes from a global namespace into a specific namespace. I
I have a specific changeset that I want to rollback my Development branch to,
I have a specific case in mind, but the question applies in general too.
I have a specific requirement that all children of a particular JComponent have double
I have a specific question, that could use a general answer... When building Multi-Tier
I have a specific Silverlight application, that is fed with data by a WCF-Service.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.