• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Swing components on the fly

 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/reflect/index.html
I think the Reflection class may give you all the answers you need.
Good Luck!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic