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?
setAlignmentY(CENTER_ALIGNMENT); // text. setHorizontalAlignment(JLabel. CENTER); // text.
JLabel does not align the text in the center.
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 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));
@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);
}
A couple JLabel
constructors take horizontal alignment arguments. These constants are inherited from SwingConstants
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With