Access an object by its name as string

W

Wiz

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..
 
H

Harald Hein

Wiz said:
I have some JTextField objects, called jtf0, jtf1, jtf2, and so
on.

No, you have object references called jtf...
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);
}

Use an array or a collection class for the references.

JTextField jtf[] = ... // fill array here
for(int i = 0; i < 10; i++) {
jtf.setText("hello" + i);
}
Does a function like this exist?

You can start to mess with the reflection API, but it is overkill.
With other languages I'd use the
eval() function, but I couldn't fint it in Java.

If you want a true equivalent of eval(), you need to fire up the
compiler from within your application. Then you load the resulting
class with a class loader. Also overkill for your problem.
 
W

Wiz

Original message from Harald Hein, Mon 22 December 2003 12:30:
No, you have object references called jtf...

yes, right :)
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);
}

Use an array or a collection class for the references.

JTextField jtf[] = ... // fill array here
for(int i = 0; i < 10; i++) {
jtf.setText("hello" + i);
}


I used this solution before, but now I'm developing an application with
netBeans. Do you know how I can use an array for references without lose
the possibility to use the gui form editor?
Thanks a lot

Marco
 
C

Christophe Vanfleteren

Wiz said:
Original message from Harald Hein, Mon 22 December 2003 12:30:
No, you have object references called jtf...

yes, right :)
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);
}

Use an array or a collection class for the references.

JTextField jtf[] = ... // fill array here
for(int i = 0; i < 10; i++) {
jtf.setText("hello" + i);
}


I used this solution before, but now I'm developing an application with
netBeans. Do you know how I can use an array for references without lose
the possibility to use the gui form editor?
Thanks a lot

Marco


Please don't multipost, I gave you an answer to the orignal question in
c.l.j.help.

Just create the array yourself and populate them with the references to the
JTextFields the gui editor creates for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top