aspose file tools*
The moose likes Java in General and the fly likes Calling object methods using reference to variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling object methods using reference to variables" Watch "Calling object methods using reference to variables" New topic
Author

Calling object methods using reference to variables

Ross Morton
Greenhorn

Joined: Jun 12, 2002
Posts: 3
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

Joined: May 25, 2001
Posts: 388
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

Joined: Jun 12, 2002
Posts: 3
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
 
subject: Calling object methods using reference to variables