Search code examples
javatextpositionjlabel

Java Changing JLabel Text changes JLabel Position


I have a problem, I have a JLabel inside the content panel which layout is absolute X Y. The problem is that the text of my JLabel is centered on JLabel, but when I add a few letters to the text, the position of the JLabel is pushed a bit back.

Can't I fix the JLabel position and use it always for the entire application? even if the text changes.. ?

Thanks in advance ;)


Solution

  • When the horizontal alignment is set to JLabel.CENTER, the text will want to expand let and right about the center position of the label.

    This means that the label is doing exactly what the label was asked to do.

    To,over come this, you should use JLabel.LEFT as the horizontal alignment and position the label itself as you need within its parent container