Update multiple JLabels dynamically

I have several JLabels and would like to be able to update these with values stored in an array.

Jlabels: companyVals0, companyVals1, companyVals2, etc
serviceVal0, serviceVal1 etc etc

   for (int x=0; x<loopLength;x++)
   {
       companyVals+x.setText(companyArr[x]);
       serviceVal+x.setText(serviceArr[x]);
       passwordVal+x.setText(passwordArr[x]);
   }

I had hoped this loop would recognize the companyVals +x = companyVals0 the first time around, then company Vals1 next....
petehindsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sciuriwareConnect With a Mentor Commented:
You should have an array like   JLabel[] labelArray = new JLabel[sizeNeeded];
Then assign values as you create JLabels:

      JLabel k = new JLabel(); // And add it to the screen.
      labelArray[i] = k;  // Running index i

And:
      .......
      labelArray[i].setText(....................);  // That should work.

;JOOP!
0
 
sciuriwareCommented:
Put those JLabels also in an array.

;JOOP!
0
 
petehindsAuthor Commented:
I have already tried that but if I write the below where companyVals is an array which stores CompanyVals1, CompanyVals2 etc

 for (int x=0; x<loopLength;x++)
   {
       companyVals[x].setText(companyArr[x]);
       serviceVa[x].setText(serviceArr[x]);
       passwordVal[x].setText(passwordArr[x]);
   }

It then tried to run "setText" as if it was part of the array method which of course it is not.

the syntax should be something like.

jlabelname.setText(companyArr[x]);

0
 
petehindsAuthor Commented:
Thanks, Sorry for not getting back to you sooner.
0
All Courses

From novice to tech pro — start learning today.