String as JLabel name

I iterate through the ArrayList and for each value in it I want to create a new JLabel. The JLable name should be a value from my list.

ArrayList<String> fieldsList = new ArrayList<String>();

for (int numberOfFields = 0; numberOfFields <= fieldsList.size(); numberOfFields++) {

        String fieldName = fieldsList.get(numberOfFields);

        JLabel "value from list"= new JLabel(fieldName + ":"); //here, in "" the name of JLabel should be the same as fieldName

    }

      

there is a possible simple solution but i cant find it

+3


source to share


2 answers


This is how I did it:

for (String fieldName : fieldsList) {
        {
            {
                JLabel lbl = new JLabel(fieldName + ":");
                lbl.setBounds(33, counter, 130, 14);
                getContentPane().add(lbl);
            }
            counter += 25;
        }
    }

      



it works for me

0


source


you cannot do this. Alternative way to create jlabel list

ArrayList<String> fieldsList = new ArrayList<String>();

JLabel[] lablearr=new JLabel[fieldsList.size()]; //lable array

for (int numberOfFields = 0; numberOfFields < fieldsList.size(); numberOfFields++) {

        String fieldName = fieldsList.get(numberOfFields);

        lablearr[numberOfFields]= new JLabel(fieldName + ":"); 

 }

      

now if you want to call a specific jlabel you can use



  lablearr[i]

      

for example, if you want to call jlable whose name mylable:

you loop over the arraylist - fieldsList

and find the index "mylable" and call jlable with that index.

+1


source







All Articles