How can you use a string to describe an already declared variable?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tom_Bright
Posted On:   Monday, October 10, 2005 12:01 PM

Hi Everyone, I've got a GUI with a number of TextFields on it (81, to be precise). I need a way of automating the access to the textfields, so I don't need to write out the same code 80 times too many. I can think of 2 ways of going about this, but aren't really sure which (if any) are possible. option 1: the textfields are called text1, text2, text3,etc. The question is is there any way I can use a counter to get to the textfields in turn? Something like: for (int i=0;i <81;i++) { String s = "text"+i.toString(); TextField t = ....... } I know that I'd    More>>

Hi Everyone,


I've got a GUI with a number of TextFields on it (81,
to be precise). I need a way of automating the access
to the textfields, so I don't need to write out the same
code 80 times too many.


I can think of 2 ways of going about this, but aren't
really sure which (if any) are possible.


option 1: the textfields are called text1, text2,
text3,etc. The question is is there any way I can use
a counter to get to the textfields in turn?


Something like:

			
for (int i=0;i <81;i++) {
String s = "text"+i.toString();
TextField t = .......
}


I know that I'd probably have to fart around a bit with
using the Integer wrapper class, but that's not really the
problem. The problem is whether it is possible
to use a string in this way to refer to a
previously declared variable? I'm not sure if it is, and if it
is, how?



The other option, which I think is more likely,
is to go from the Frame that the TextFields appear on and
find all the components on it. Then it might be possible
to use a counter as before to get to the right TextField
name as a string, then say 'get me the TextField on
this Frame where its name equals this string'.


I'd be really grateful for a bit of advice.


Thanks for your time,

Tom

   <<Less

Re: How can you use a string to describe an already declared variable?

Posted By:   WarnerJan_Veldhuis  
Posted On:   Tuesday, October 11, 2005 03:41 AM

In your GUI, create a List of TextFields. Iterate over that list.

List fields = new ArrayList();
fields.add( someFieldHere);
fields.add( someOtherFieldHere);
while ( someLoop throuogh databse) {
fields.add( someField);
}

....

//iterate over the fields (jdk5-style)
for( Field f : fields ) {
//f is now a field in the list
}

//iterate over the fields (jdk4-style)
for( Iterator iter = fields.iterator(); iter.hasNext(); ) {
Field f = (Field) iter.next();
//f is now a field in the list
}


About | Sitemap | Contact