File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling object methods using reference to variables
 
Similar Threads
Scripting with JSTL Core tags.
//// is not working in MSXML
Possible to dynamically declare and name variables?
Using form arrays within the javascript of a .jsp
Reflecting XML into Java Objects