JLabel inside for-loop acting irratic

J

Justin

I have a for-loop that goes through an ArrayList and a String array and
creates a new JLabel. It is then added to a JPanel and displayed.

Below is my code:

for(int x = 0; x < problemListCount; x++){

for(int y = 0; y < 3; y++){

JLabel label = new JLabel(problemsList[x][y]);
problems.add(label);
label.setBounds(xDistance,
yDistance,
label.getPreferredSize().width,

label.getPreferredSize().height);

System.out.println("For pass " + passCount++);
System.out.println("xDistance = " + xDistance);
System.out.println("yDistance = " + yDistance);

xDistance = xDistance + label.getPreferredSize().width
+ 10;

System.out.println(label.getBounds().x + " " +
label.getBounds().y);

}

xDistance = 10;
yDistance = yDistance + 30;

Now what this should do, is print out the items in a grid with 3
columns (there are 3 fields in the inner array). However, when I run
it, the JLabels form a straight row until the loop is complete. The
system.out statements show a normal progression of the values of both
the xDistance and yDistance variables as well as the getBounds, which
is why this is so vexing to me.

Oh ya, I'm also a newb, perhaps I cant put a label in a loop like that
expecting multiple labels to show up?

If so, how would I print information from a ListArray where I wont know
the length of the Array, but need to be able to edit the information.

Thanks
 
O

Oliver Wong

Justin said:
I have a for-loop that goes through an ArrayList and a String array and
creates a new JLabel. It is then added to a JPanel and displayed.

Below is my code:

for(int x = 0; x < problemListCount; x++){

for(int y = 0; y < 3; y++){

JLabel label = new JLabel(problemsList[x][y]);
problems.add(label);
label.setBounds(xDistance,
yDistance,
label.getPreferredSize().width,

label.getPreferredSize().height);

System.out.println("For pass " + passCount++);
System.out.println("xDistance = " + xDistance);
System.out.println("yDistance = " + yDistance);

xDistance = xDistance + label.getPreferredSize().width
+ 10;

System.out.println(label.getBounds().x + " " +
label.getBounds().y);

}

xDistance = 10;
yDistance = yDistance + 30;

Now what this should do, is print out the items in a grid with 3
columns (there are 3 fields in the inner array). However, when I run
it, the JLabels form a straight row until the loop is complete. The
system.out statements show a normal progression of the values of both
the xDistance and yDistance variables as well as the getBounds, which
is why this is so vexing to me.

Oh ya, I'm also a newb, perhaps I cant put a label in a loop like that
expecting multiple labels to show up?

If so, how would I print information from a ListArray where I wont know
the length of the Array, but need to be able to edit the information.

It looks like you got your for loops inverted. The outer should iterate
through the Ys while the inner should iterate through the Xes.

Incidentally, you should very strongly consider using GirdLayout:
http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top