• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling object methods using reference to variables  RSS feed

 
Ross Morton
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 ?
 
karl koch
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!