You can try to instantiate the class as follows
 
if (componentType.equals("JButton"))
{
   JButton compName = (JButton) Class.forName("JButton").getConstructor(new Class[]{String.class}).newInstance(new Object[]{label});
 
regards
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Alex Kravets
Sent: Wednesday, January 09, 2002 4:08 PM
To: [EMAIL PROTECTED]
Subject: Creating Swing components on the fly

Hello,
Does anybody know how to create Swing components on the fly?
Here is the thing. I am reading a file in my applet that has information about potential Swing components I am going to need. Each line of that file describes characteristics of that components.
So, if my line reads:
==================================
button|test|JButton
text|sample text|JTextField
==================================

I need automatically to generate two Swing components: JButton and JTextField.
I have a class that reads this file, breaks each line into tokens and initializes that class's private variables to all these tokens. So in the end, I have an array containing objects that have information:
name = button;
label = test;
componentType = JButton;

Now, here is a problem, in my applet I get this array with objects, but how do I actually create Swing objects on the fly? I tried doing something like this:

String componentType = (String)tbData[0].getComponentType();
String label = (String)tbData[0].getLabel();
String compName = (String)tbData[0].getName();
if((componentType.equals("JButton"))){
componentsVector.add(JButton compName = new JLabel(label));

It looks crazy, so does not work so far.

May be someone has any ideas on how to do this, I would appreciate any response.

thanks,
Alex

Reply via email to