• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoke method with reflection

 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you need reflection.
can you not use nmp.setText("cool");
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make setter methods for each TextFields.
Here is an example setting a string :

 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok

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

 
Geoffrey Laurens
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic