6

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.

Grammin
  • 11,808
  • 22
  • 80
  • 138
  • 2
    Have a look at this example - http://www.java2s.com/Code/Java/Swing-JFC/AsimpledemonstrationoftextalignmentinJLabels.htm – mre Jun 06 '11 at 19:52

9 Answers9

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

:)

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
dalvarezmartinez1
  • 1,385
  • 1
  • 17
  • 26
6

The setPreferredSize/MinimumSize/MaximumSize methods is dependent from the layout manager of the parent component (in this case panel).

First try with setMaximumSize instead of setPreferredSize, if I'm not going wrong should work with BoxLayout.

In addition: probably you have to use and play around with glues:

panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());

If you need the Y_AXIS BoxLayout you could also used nested panel:

verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));    
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());
verticalPanel.add(panel);
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • The second snippet leads to the "BoxLayout can't be shared" exception. Should be verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); – ka3ak Aug 08 '12 at 16:19
5

I think it depend to layout that you are using, in XY (I remember was some kind of layouts in JBuilder) it should work, but in other can be problem. Try to change minimum size to prefered size.

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
3

This is a bit annoying, but you could use nested JPanels with box layouts if you wanted more flexibility in your alignment than with grid layout.

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


    for (int xx = 0; xx < 3; xx++) {
        JPanel temp = new JPanel();
        temp.setLayout(new BoxLayout(temp,BoxLayout.LINE_AXIS));

        JLabel label = new JLabel("String");
        temp.add(Box.createHorizontalGlue());

        temp.add(label);
        panel.add(temp);
    }

I used horizontal glue to keep it at the right no matter the size, but you can put in rigid areas to make it a specific distance.

jhlu87
  • 3,999
  • 8
  • 38
  • 48
2

You need to ensure that your LayoutManager is sizing the label to fill the target area. You probably have a JLabel component which is exactly sized to the length of the text and which has been left aligned in the layout.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
2
myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • the problem here is not the aligment itself. The problem is that the JLabel hasn't the expected size, so the aligment effect is not visible. – Heisenbug Jun 06 '11 at 20:11
  • @0verbose BoxLayout accepted PreferredSize, if space are shared, but who did you tell that all JComponets in Container must be visible – mKorbel Jun 06 '11 at 20:19
1

rather than use

label.setHorizontalAlignment(JLabel.RIGHT);

use

label.setHorizontalAlignment(SwingConstants.RIGHT);

thus you have:

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(SwingConstants.RIGHT);
    panel.add(label);
}
nepaluz
  • 109
  • 2
  • 1
1

Couldn't you use the following?

Jlabel label = new JLabel("String");
label.setBounds(x, y, width, height); // <-- Note the different method used.
label.setHorizontalAlignment(JLabel.RIGHT);

This works within a JFrame Container at least. Not sure about a JPanel.

Zane
  • 11
  • 1
0

With the responses from you guys I was able to determine that BoxLayout doesn't support the text alignment that I wanted, so I changed it to

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,0,0);

and everything worked fine.

Grammin
  • 11,808
  • 22
  • 80
  • 138