Archit Verma
Archit Verma

Reputation: 1909

How to align JLabel to the left of the JPanel?

I want to align my JLabel to the left.

    String lText = "<html><b><font color = white face = comic sans ms size = 20>mybook</font></b></html>";
    JLabel label = new JLabel(lText);
    label.setBorder(new EmptyBorder(25,0,25,500));

I tried to do it using EmptyBorder but it isnt aligning properly. I am using FlowLayout

Upvotes: 10

Views: 35886

Answers (2)

Reimeus
Reimeus

Reputation: 159864

FlowLayout uses CENTER alignment by default. Try using LEFT alignment for your JLabel JPanel container

myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

Upvotes: 15

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You might wish to set the JLabel's horizontalAlignment property. One way is via its constructor. Try:

JLabel label = new JLabel(lText, SwingConstants.LEFT);

This can also be done via the expected setter method:

label.setHorizontalAlignment(SwingConstants.LEFT);

Upvotes: 4

Related Questions