0

For displaying 10 labels in a JPanel I used the following code using Netbeans, for Java. Not getting the any output. JPanel layout set as null layout.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   JLabel[] labels = new JLabel[10];
   for(int i=0; i<9; i++){
   labels[i] = new JLabel("Label Name " + i);
   p1.add(labels[i]);}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • is the panel added to the frame? – Terry Storm Oct 29 '14 at 09:49
  • What is p1? have you defined it? – Rafa Romero Oct 29 '14 at 09:49
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Oct 29 '14 at 09:53
  • *"`JPanel` layout set as `null` layout."* Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 29 '14 at 09:55

1 Answers1

3

You need to call the validate method on the frame object after adding subcomponents to it.

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

M A
  • 71,713
  • 13
  • 134
  • 174