Harindra Singh
Harindra Singh

Reputation: 371

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?

Upvotes: 2

Views: 3273

Answers (3)

Sachin Mhetre
Sachin Mhetre

Reputation: 4543

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.

Upvotes: 4

Pawan Sharma
Pawan Sharma

Reputation: 3354

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

Upvotes: 0

KrHubert
KrHubert

Reputation: 1030

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]);
}

Upvotes: 0

Related Questions