简体   繁体   中英

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 ?

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("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM