Skip to content

Align JLabel using SwingConstants

Why is my JLabel text not aligned to the left or center? Is the FlowLayout the issue? Both of the texts are just appearing at the top, next to each other and I can’t fix this.

Main Class

public static void main(String[] args) {
    simpleGui SG = new simpleGui();

Constructor class

public class simpleGui extends JFrame{
    private JLabel label1;
    private JLabel label2;
        //title bar
        super("Simple GUI");
        setLayout(new FlowLayout());        
        label1 = new JLabel("I'm a label in the window");
        label2 = new JLabel("Label2");



Refer to Laying Out Components Within a Container which is a lesson in the Creating a GUI With Swing trail of Oracle’s Java tutorials.

The javadoc for method setVerticalTextPosition, of class JLabel states the following:

Sets the vertical position of the label’s text, relative to its image.

A JLabel can contain both text and an image. The image is also referred to as an icon. Since the JLabel in your code contains only text, calling this method does nothing. The same applies for method setHorizontalTextPosition.

A layout manager is responsible for placing a JComponent within its parent container. FlowLayout, by default, places the components in the top, center part of the container (which is JPanel in your code) and gives them their preferred size. (Refer to method getPreferredSize in class javax.swing.JComponent)

I suggest that you put a border around each component so that you can see how much space each one actually takes up. In the below screen capture, I added a red border to the [content pane of the] JFrame, a cyan border around label1 and a blue border around label2.

screen capture 1

I assume that you want label1 to appear at the bottom of the content pane and label2 to appear at the left side. Therefore I think that the default layout manager is appropriate. The default layout manager is BorderLayout.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class SimpleGui extends JFrame {
    private JLabel label1;
    private JLabel label2;

    public SimpleGui() {
        super("Simple GUI");
        ((JPanel) getContentPane()).setBorder(BorderFactory.createLineBorder(;
        label1 = new JLabel("I'm a label in the window", SwingConstants.CENTER);
        add(label1, BorderLayout.PAGE_END); // changed

        label2 = new JLabel("Label2");
        add(label2, BorderLayout.LINE_START); // changed

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            SimpleGui sg = new SimpleGui();

This is how it looks when I run the above code.

screen capture 2

Note that BorderLayout gives the bottom component the same width as the content pane as well as its preferred height while the left component is given its preferred width but its height matches the height of the content pane. Refer to the tutorial I linked to at the start of this answer.

Since the width of label1 is greater than the width of the text it contains, I explicitly set the horizontal alignment of label1 to CENTER. By default it is LEFT. The alignment only affects the display when the width of the JLabel is larger than the width of its text.

I recommend that you go through the entire Creating a GUI With Swing trail in Oracle’s Java tutorials.

User contributions licensed under: CC BY-SA
2 People found this is helpful