This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes invoke method with reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "invoke method with reflection" Watch "invoke method with reflection" New topic
Author

invoke method with reflection

Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
i have a class, it has several field/variable/instance variable.
example :

public class FirstApplet extends JApplet {

private JPanel jContentPane;
private JTextField npm;
private JTextField name;

}

how can i invoke setText on npm, name automaticly with reflection???

this is my code :

Field[] fields = getClass().getDeclaredFields();
for(int i = 0; i < fields.length; i++){
System.out.println(fields[i].getName() + ", " + fields[i].getType().getName());
if(fields[i].getType().getName().equals("javax.swing.JTextField")){
System.out.println(fields[i].getName());
Method[] methods = fields[i].getType().getMethods();
for(int j = 0; j < methods.length; j++){
if(methods[j].getName().equals("setText")){
System.out.println(methods[j].getName());
try {
Method m = fields[i].getType().getMethod(methods[j].getName(), new Class[] {String.class} );
m.invoke(fields[i].getType().newInstance(), new Object[]{"geox"});
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

why it's not working......
please help me
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
why do you need reflection.
can you not use nmp.setText("cool");


Rahul Bhattacharjee
LinkedIn - Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

why it's not working......
please help me


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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You could make setter methods for each TextFields.
Here is an example setting a string :



[My Blog]
All roads lead to JavaRanch
Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
Originally posted by Satou kurinosuke:

i think we have a miss understanding here.....

what i'm trying to invoke is the setText(String) that JTextField already have in it. so the setText(String) is not build by setter and getter. but JTextField already have a method setText(String).

i want to ask you guys, example :

i declare

JTextField address(an attribute javax.swing.TextField with a name address)

i can get that attribute by reflection like

Field[] f = getClass().getDeclaredFields();

f[0] will have value address

this is the question is it "f[0]" is the same with "address instance" ???
but what i think is they are not the same.....


this is my example source code :

Class FirstApplet


and class UtilsReflection

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

i think we have a miss understanding here.....

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 ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you want to modify the value directly calling invoke(), I think you'll have to pass a reference to your TextField.
Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
i know Mr. Satou kurinosuke.....

but i have to create the setter and getter in every JApplet class that have made.
what i want is i don't have to create the setter and getter....

this is what i had in mind...

me create a method that take parameter of to Object like Person and the JApplet. may be little bit like this example....



and in the "bindApplet" applet method, the method will search all field in JApplet that have a type of "javax.swing.JTextField" and if it find it, it will invoke the setText method.......

so the "bindApplet" can be use not just for one JApplet but can be use for all JApplet.....

or is it has to be just like you said, i must created setter and getter in every JApplet class that i created???

if it's has to be done like that, why don't i just do this .....



so what do you think???

thanks before Mr. Satou kurinosuke
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

ok

What about this ? You must have a reference to the applet, right ?

Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
thanks a lot man (Satou kurinosuke)



hehehehe

i'm new in java, sorry if it's a silly question to you hehehehe
Geoffrey Laurens
Greenhorn

Joined: Sep 19, 2006
Posts: 24
one other question is it suggested to use reflection in JApplet or other java code, as far as i know. it's kind a took a lot of time to process it. or may be i'm wrong?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

i'm new in java, sorry if it's a silly question to you hehehehe

There's no silly question
It's a pretty advanced topic for somebody who is new to java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invoke method with reflection
 
Similar Threads
reflection to get values in an interface
invoke method with java reflection
Reading Annotations without using instanceof
Need help on JTree
Getting value of static variable with Reflection ?