How to check objects in a java cycle? [closed]

2

I wanted to know if it is possible to do a validation, for example, if I have 3 jtextfield with serial name ejm:
jTextField1
jTextField2
jTextField3
(obviously more)
and I want to see what they have stored, what can I do in a cycle? and in the event that yes, how could he do it?

    
asked by Daynom 27.03.2017 в 18:29
source

1 answer

2

The only way you can iterate over variables (fields) named in a similar way is to use reflection with the class.getDeclaredField(String name) method.

Using reflection is generally slow, so it would be better to save your variables in an array or a list to start with:

List<JTextField> textFields = new ArrayList<JTextField>();
textFields.add(new JTextField());
// luego se puede usar un iterator para recorrer los jTextField
Iterator<JTextField> it = textFields.iterator();
while (it.hasNext()){
    JTextField tf = it.next();
    // lo que quieres hacer con tf...
}
    
answered by 27.03.2017 / 19:38
source