Hi there,
I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
How I can access their properties by their names known as string? For
instance:
for(int i=0; i<10; i++) {
function_to_get_a_reference("jtf" + i).setText("hello " + i);
}
Does a function like this exist? With other languages I'd use the eval()
function, but I couldn't fint it in Java.
Thanks in advance to everyone can help me.
Bye
Marco
ps: sorry for my bad english..
Christophe Vanfleteren - 22 Dec 2003 12:02 GMT
> Hi there,
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
[quoted text clipped - 13 lines]
>
> ps: sorry for my bad english..
No, you can't do that (not without using something like reflection anyway,
which would be a bad solution for things like this).
Just put all your textfields in an array.
something like:
JTextField[] txtfields = new JTextField[3];
//arrays must be explicitly initialized for objects:
for (int i = 0;i<txtfields.length;i++) {
txtfields[i] = new JTextField();
}
Later on you loop over that same array and set the text:
for (int i = 0;i<txtfields.length;i++) {
txtfields[i].setText("Hello " + (i+1));
}

Signature
Kind regards,
Christophe Vanfleteren
Antti S. Brax - 22 Dec 2003 12:09 GMT
wiz@near.it wrote in comp.lang.java.gui:
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
> How I can access their properties by their names known as string? For
> instance:
<snip>
Let me get this straight: You have a class which has attributes
named "jtf0", "jtf1" and so on. These attributes are of type
JTextField and contain references to text fields displayed in
your GUI. Right?
The wrong way to do it is to declare the attributed public and
use reflection. Class.getField(String) returns a Field object
which has a get(Object o) method. This method returns the value
of the specified field in object o.
The right way is to it is to redesign the GUI a bit and put the
JTextField objects into a Map with appropriate keys. Or if all
the fields perform a similar function you could put them into
an array.
You have an idea about how you want to do something. Please tell
us what you want to do because the "how" you have figured out is
propably not a very good idea...

Signature
Antti S. Brax - asb(at)iki.fi Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/
Vincent Vollers - 22 Dec 2003 12:26 GMT
> Hi there,
> I have some JTextField objects, called jtf0, jtf1, jtf2, and so on.
[quoted text clipped - 13 lines]
>
> ps: sorry for my bad english..
Hi Marco,
I think the best solution would be to use an array instead of dynamicly
accessing fields.
If you wish to access the fields of the current class you can do something
like this:
// to get/set a public/private property
this.getClass().getDeclaredField("fieldName").set(this, "newValue");
Object val = this.getClass().getDeclaredField("fieldName").get(this);
soo... in your case it would become something like this
for(int i=0;i<10;i++) {
JTextField field = (JTextField) this.getClass().getDeclaredField("jtf" +
i).get(this);
field.setText("hello " + i);
}
I don't know how to access temporary variables though. (In other words, the
above method works for properties of objects only, not variables declared
within a function)
Regards,
- Vincent