Ahmed Bin Abdullah
Ahmed Bin Abdullah

Reputation: 19

how to clear all jTextField in for loop?

i have 30 jtextfield: jTextField1..through..jTextField30 ; to avoid redundancy i'm trying to clear all fields in for loop such that:

          for (int i=1;i<31;i++)
                {
                    jTextField(String.valueOf(i)).setText("")

                }

but it is error in this way , how to do it ?

Upvotes: 0

Views: 68

Answers (1)

3kings
3kings

Reputation: 838

So you need something like this

ArrayList<JTextField> fields = new ArrayList<JTextField>(); //This needs to be populated with your JTextFields
for(int i = 0; i < fields.size(); i++)
    if(fields.get(i) != null)
        fields.get(i).setText("");

Upvotes: 1

Related Questions