We help IT Professionals succeed at work.

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++)

I had hoped this loop would recognize the companyVals +x = companyVals0 the first time around, then company Vals1 next....
Watch Question

Put those JLabels also in an array.



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++)

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.


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

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



Thanks, Sorry for not getting back to you sooner.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.