简体   繁体   中英

setting jTextField values dynamically

First, I'm new to java. Lately encountered a problem like this.

I have an integer array like,

 int[] data={32,12,31,89,90};

then, I have 5 jTextFields.

I named them as jNum0,jNum1,...,jNum4. Problem is that according to the program I'm working on

I need to print each value in array in respective textbox by using a loop .

Like:

for(int i=0;i<=4;i++){ 
//<jNum+i>.setText(data[i]);
// This actually doesn't work
}

Is there a way to do this?

Better create an 'array of TextField'

TextField tf[] = new TextField[5];

And after this, call for loop as

for(int i=0;i<=4;i++)
{
     tf[i].setText(data[i]);
}

It will work.

Try to solve by creating an array of JTextFields and refer each JTextField by its index. You will be able to access each JTextField by its index and your problem would be solved.

jTexts are just classes and like any other class in Java you can easily create an array of objects of type jText.

For more help use this link:

Creating array of JTextFields

You have hata in array. Put jTextFields in array too.

JTextField[] fields = new JTextField[5];
for(int i = 0; i < fields.length; i++) {
    fields[i] = new JTextField();
}

for(int i = 0; i < fields.length; i++) {
    fields[i].setText(data[i]);
}

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