Skip to content
Advertisement

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();
    SG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SG.setSize(1000,1000);
    SG.setVisible(true);
}

Constructor class

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

Advertisement

Answer

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(Color.red));
        label1 = new JLabel("I'm a label in the window", SwingConstants.CENTER);
        label1.setBorder(BorderFactory.createLineBorder(Color.cyan));
        add(label1, BorderLayout.PAGE_END); // changed

        label2 = new JLabel("Label2");
        label2.setBorder(BorderFactory.createLineBorder(Color.blue));
        add(label2, BorderLayout.LINE_START); // changed
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            SimpleGui sg = new SimpleGui();
            sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sg.setSize(1000,1000);
            sg.setVisible(true);
        });
    }
}

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
Advertisement