Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I right align text within a JLabel?

I have the following code:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

for(int xx =0; xx < 3; xx++)
{
    JLabel label = new JLabel("String");
    label.setPreferredSize(new Dimension(300,15));
    label.setHorizontalAlignment(JLabel.RIGHT);

    panel.add(label);
}

This is how I would like the text to look:

[                         String]
[                         String]
[                         String]

this is how it looks

[String]
[String]
[String]

For some reason label doesn't get set to the preferred size I specified and I think because of this it doesn't right align my label text. But im not sure. Any help would be appreciated.

like image 483
Grammin Avatar asked Jun 06 '11 19:06

Grammin


1 Answers

JLabel label = new JLabel("String", SwingConstants.RIGHT);

:)

like image 113
dalvarezmartinez1 Avatar answered Sep 29 '22 00:09

dalvarezmartinez1