Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 250
Archit Verma Avatar asked Jul 07 '13 10:07

Archit Verma


2 Answers

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

myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
like image 115
Reimeus Avatar answered Oct 16 '22 16:10

Reimeus


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);
like image 38
Hovercraft Full Of Eels Avatar answered Oct 16 '22 18:10

Hovercraft Full Of Eels