Text align in JLabel

Ariel Grabijas picture Ariel Grabijas · Dec 28, 2011 · Viewed 61.1k times · Source

I've got GridLayout-JPanel. In every cell there is a JLabel with some String. How can I right-align this text in my cells?

Answer

fireshadow52 picture fireshadow52 · Dec 28, 2011

@Noran In response to your comment on @mre's answer, you could initialize all the JLabels into an array. Then, all you'd have to do is loop through the array and set the alignment that way.

for (JLabel label: arrayOfJLabels) {
    label.setHorizontalAlignment(SwingConstants.LEFT);
}