This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling object methods using reference to variables

 
Ross Morton
Greenhorn
Posts: 3
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic