1

How can I create a Swing component ( such as a JPanel or JButton ) dynamically from a string that refers a component name?

For example;

String c = "JPanel";

(c) com = new (c)(); // It must be equivalent JPanel com = new JPanel();

or a function like this;

Object c = Object.createObjectFromString(c);

Thanks

11
  • Why would you want to do this? All the validation you are going to need will just make your code unreadable. Commented Oct 21, 2012 at 19:09
  • I suppose this could be done with reflection, but could you give us more context here? Why do you need/want this functionality? Commented Oct 21, 2012 at 19:09
  • Well, it can be done with Class.forName() and then calling appropriate constructor, but why would you need this ? Commented Oct 21, 2012 at 19:10
  • Looks like we're all smelling the same bad code smell here. Commented Oct 21, 2012 at 19:10
  • I am smelling, write your own preprocessor for java smell. Commented Oct 21, 2012 at 19:11

3 Answers 3

3

It can be done, using the Reflection API:

Class<?> klazz = Class.forName("javax.swing.JPanel");
JPanel panel = (JPanel) klazz.newInstance();

Notice that you have to pass a fully-qualified class name to the forName() method, and that at some point (be it as a type parameter to Class<?> or by using a cast like in the above code) you'll have to explicitly specify the class that you intend to instantiate. Or if you don't care about the exact type of the instance, you can simply do this:

Object obj = klazz.newInstance();

Also, I'm assuming that the class has defined a no-arguments constructor. If that is not the case, you'll have to create a Constructor object first before instantiating the new object:

Class<?> klazz = Class.forName("javax.swing.JPanel");
Constructor<?> constructor = klazz.getDeclaredConstructor(/* parameter types */);
JPanel panel = (JPanel) constructor.newInstance();
Sign up to request clarification or add additional context in comments.

Comments

2

You could use reflection:

// c is JPanel for instance
Sting componentClassName = "javax.swing." + c; 
JPanel panel = (JPanel)Class.forName(componentClassName).newInstance();

Using Class.newInstance() requires a the component you are creating to have a default constructor but this is provided by most if not all the standard Swing components.

2 Comments

But, I don't know what is it type. It could be JButton
@miqbal: that's where the String parameter comes in. Use a variable there and your problem is solved.
0

see here How to create an object from a string in java how to eval a string and Creating an instance from string in java. But I would also suggest you to write your own preprocessor if you want it to evaluate in 1 single line. That's what C/C++ preprocessors do.

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.