swing - How to clear text fields using for loop in java -
i created text fields in java following. when click "clear" button want clear of these text fields @ once.
private javax.swing.jtextfield num1; private javax.swing.jtextfield num2; private javax.swing.jtextfield num3; private javax.swing.jtextfield num4; private javax.swing.jtextfield num5; private javax.swing.jtextfield num6; private javax.swing.jtextfield num7;
now want know how use loop clear these text fields like:
for(int i=1;1<7;i++){ num[i].settext(null); }
code this:
private javax.swing.jtextfield num1; private javax.swing.jtextfield num2; private javax.swing.jtextfield num3; private javax.swing.jtextfield num4; private javax.swing.jtextfield num5; private javax.swing.jtextfield num6; private javax.swing.jtextfield num7;
is code crying out arranged , simplified using collections or arrays. if use array of jtextfield or perhaps better arraylist<jtextfield>
. clearing them trivial.
public static final int field_list_count = 7; private list<jtextfield> fieldlist = new arraylist<jtextfield>(); // in constructor (int = 0; < field_list_count; i++) { jtextfield field = new jtextfield(); fieldlist.add(field); fieldholderjpanel.add(field); // jpanel holds text fields } // clear method public void clearfields() { (jtextfield field : fieldlist) { field.settext(""); } }
Comments
Post a Comment