javax.swing.JButton, you can use following code to create
component object on the fly using this "componentClassName".
Class componentClass = Class.forName(componentClassName);
//
Get the public Constructor for this class based on what you read from
// your input file
Class[] args = new Class[1];
//assuming that you are calling the constructor which
// uses String arg only
args[0] = String.class;
Constructor ct = componentClass.getConstructor(args);
//Instantiate
this class using this constructor
Object[] oArgs = new Object[1];
oArgs[0] = "stringArgumentValue";
Object o = ct.newInstance(oArgs);
Hope this helps ..
- Ajit
Alex Kravets wrote:
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
