0

what's wrong with this? the labels[] causes an error

JLabel labels[] = new JLabel();
    for (int i =  0; i < 4; i++) {
       labels[i] = new JLabel("Label" + i);
       panel.add(labels[i]);
 }

4 Answers 4

2
JLabel label = new JLabel(); 

initialize a single Jlabel if you want to initialize array you should do like that

JLabel labels[] = new JLabel[4];
Sign up to request clarification or add additional context in comments.

1 Comment

it is not related to with that code maybe there errors after that
2
JLabel labels[] = new JLabel[4];

and then you have to create new instances for each array entry (otherwise array contains only nulls)

for(JLabel label : labels) {
  label = new JLabel();
}

Comments

0

First of all you must define the array. Then you can play with the methods in it.

Labels = new JLabel[]{ label1, label2, label3 };

for(int i=0; i<Labels.length; i++){
         add(Labels[i]);
}

Comments

0

there is a problem with declaring JLabel array,

JLabel labels[] = new JLabel(); //Incorrect code
JLabel[] labels = new JLabel[enter the size]; //Correct One

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.