Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text align in JLabel [duplicate]

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?

like image 918
Ariel Grabijas Avatar asked Dec 28 '11 16:12

Ariel Grabijas


People also ask

How do you center text in a JLabel?

setAlignmentY(CENTER_ALIGNMENT); // text. setHorizontalAlignment(JLabel. CENTER); // text.

Which of the following alignment is not possible for JLabel?

JLabel does not align the text in the center.

How are text only labels aligned by default in the display area?

By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default. You can also specify the position of the text relative to the image.

How do I increase the font size of a JLabel?

How do I set font size in Java? We change the Font Size parameter to change the size of the JLabel Font. The use of the Font() function is shown below: Object. setFont(new Font("Font-Style", Font-Weight, Font Size));


2 Answers

@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);
}
like image 146
fireshadow52 Avatar answered Oct 23 '22 07:10

fireshadow52


A couple JLabel constructors take horizontal alignment arguments. These constants are inherited from SwingConstants.

like image 22
mre Avatar answered Oct 23 '22 07:10

mre