Question:
Setting JLabel location in Java?
?
2012-08-10 11:13:42 UTC
I've created an array of 5 JLabels that I want to display sortof like a list. They all display perfectly except for the last one. For some reason, the last label displays in the middle of the JFrame, all the way left. My code is:

JLabel[] labels = new JLabel[items.chemicals.length];
int labelLocX = 10;
int labelLocY = 100;
for(int i = 0; i < labels.length; i++){
labels[i] = new JLabel(items.chemicals[i].getName() + " " + money.format(items.chemicals[i].getPrice()));
labels[i].setBounds(labelLocX, labelLocY, 500,20);
labelLocX =10;
labelLocY +=20;
add(labels[i]);


}

And it displays with :
. Item
. Item
. Item
. Item


Item

^ the last one down a few spaces and to the left. Why?!
Three answers:
anonymous
2012-08-14 04:25:44 UTC
This site should help: http://www.dreamincode.net/forums/topic/51368-set-location-for-jlabel/
Radu
2012-08-14 05:44:55 UTC
It is not down a few spaces. Actually it is on the centre of the JFrame. Try to resize the window and see the difference.



To avoid this, set the Layout of your Frame to null.

setLayout(null); //this line should be written before the for loop.



Hope this helps!

Good luck!
kapsner
2017-01-15 22:55:56 UTC
i'd use Java DND, they made each and all the paintings effortless so you might apply. All you may desire to handle is Telling it what to tug, and to do mutually as dragging (including teach text cloth, or make some noise), and what to do as quickly as you end dragging (like make it colorized, etc). quite effortless to do as quickly as you get your arms on it!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...