Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem regarding loops. I need to access 10 labels through a loop in java?

Tags:

java

loops

I have a problem regarding loops. I need to access 10 labels which have names like label1, label2, label3 .... etc. I need to know whether I can access those labels by going through a loop in java?

like image 210
maduranga Avatar asked Dec 06 '22 19:12

maduranga


2 Answers

How about using List or an array

List<JLabel> labels = new ArrayList<JLabel>();
labels.get(index);
like image 181
jmj Avatar answered Dec 10 '22 12:12

jmj


Change those labels to be an array, and access it using an index.

For example:

JLabel[] labels = new JLabel[10];
for (int i = 0; i < labels.length; ++i) {
    labels[i] = new JLabel("Label " + i);
}
for (int i = 0; i < labels.length; ++i) {
    // access each label.
}
like image 37
MByD Avatar answered Dec 10 '22 11:12

MByD