Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Calling object methods using reference to variables  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JPanel containing a number of JText fields, and would like to set the Name property of these fields to be the same as their variable names. I am currently doing :
tfName.setName("tfName");
tfAddress.setName("tfAddress"); etc. etc. !
I would like to do this in a loop without specifying the actual names -
I can get the variable names by using Reflection :
Public void getFieldNames(JPanel PassPanel) {
Class c = this.getName();
Field[] f = c.getDeclaredFields();

<snip > fieldName[i] = f[i].getName(); </snip>
How can I use the setName method on these variables ?
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have written a small sample. it should give you an idea how you can achieve it. just copy/paste the whole code in a file named Intro.java, compile and run it.
k

[ May 12, 2003: Message edited by: karl koch ]
 
Ross Morton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl -
Thanks very much! That is *exactly* what I have been trying to do, and it works perfectly
I've been trying to do it by RTFM and it's been driving me crazy ...
Best wishes
Ross
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!