Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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]); }
JLabel label = new JLabel();
initialize a single Jlabel if you want to initialize array you should do like that
JLabel labels[] = new JLabel[4];
Add a comment
and then you have to create new instances for each array entry (otherwise array contains only nulls)
for(JLabel label : labels) { label = new JLabel(); }
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]); }
there is a problem with declaring JLabel array,
JLabel labels[] = new JLabel(); //Incorrect code JLabel[] labels = new JLabel[enter the size]; //Correct One
Required, but never shown
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.
Explore related questions
See similar questions with these tags.