Probably because the field is private. Please explain what you mean with "it's not working". What do you expect to happen and what happens when you run this, and how is this different from what you expect?
example : 1. first i have a model Person with a field String name, String address 2. second i have a JApplet that have an attribute JTextField nama, JTextField address. 3. in the database i have a table person with field name and address. 4. i want to get 1 record from database and transfer it into Person Model 5. i search from JApplet that has the same field/attribut name like Person. that is JTextField name = String name and JTextField address = String address. 6. How do i transfer the value of each field by java reflection, so that the value transfer is done by automaticly not manualy. thats the idea..... so i don't have to do this "name.setText("geox")" and "address.setText("NY")", but i can do it by reflection just by getting all field from JApplet and Person and if they have the same name than i set from the Person into JApplet... hehehe
This kind of thing is pretty common in data binding utilities. For example a tool might parse XML to build an object, find a tag called "name", use reflection to find a method called "setName(String)" and invoke the method.
Note how this brings together three things you have to know: the object to work on, the field name and the value. Does that sound like your situation?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
You still haven't said how your existing code fails, so debugging it is not gonna happen.
However, it sounds like you may need to look into JavaBeans. That provides a way of calling getter and setter methods, according to a property name. It actually works with reflection, but without the bugs! If JavaBeans does sound like it does what you want, consider the Apache Jakarta Bean Utils freeware.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Sep 19, 2006
there is no error in my java code, but the result is not the way i expected.
example : in this java code
i get a result : "npm, javax.swing.JTextField"
and in this java code :
it will invoke setText(String) method that its in JTextField class. but the JTextField npm is still empty not fill with "geox".
but if i change the code like this : "fields[i].getType().newInstance()" i change it with "npm"
then the JTextField npm will fill with "geox" and it's not empty. thats what i wanted....
i don't want to write "npm" directly but i want to get the field "npm" by reflection.....
i'm sorry if all you guys a little bit confuse with my explanation. i'm not very good in explaining things.
Oh no, no misunderstanding here Does it makes sense to look through all methods ? You already know you want to call "setText".
So I was suggesting you to call setText in a member method like :
So if you get the name of the field with reflection, you can set its value by calling the setter method above. This is how Spring works to set member variables. [ September 21, 2006: Message edited by: Satou kurinosuke ]