How do I right align text within a JLabel?
Asked Answered
B

9

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.

Beira answered 6/6, 2011 at 19:42 Comment(1)
Have a look at this example - java2s.com/Code/Java/Swing-JFC/…Cyrille
F
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);
Filemon answered 6/6, 2011 at 19:54 Comment(1)
The second snippet leads to the "BoxLayout can't be shared" exception. Should be verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));Balikpapan
R
13
JLabel label = new JLabel("String", SwingConstants.RIGHT);

:)

Reinke answered 27/2, 2013 at 14:6 Comment(0)
F
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);
Filemon answered 6/6, 2011 at 19:54 Comment(1)
The second snippet leads to the "BoxLayout can't be shared" exception. Should be verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));Balikpapan
U
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.

Unfaithful answered 6/6, 2011 at 19:48 Comment(0)
S
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.

Smythe answered 6/6, 2011 at 20:16 Comment(0)
N
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.

Narvik answered 6/6, 2011 at 19:56 Comment(0)
V
2
myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
Verb answered 6/6, 2011 at 20:9 Comment(2)
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.Filemon
@0verbose BoxLayout accepted PreferredSize, if space are shared, but who did you tell that all JComponets in Container must be visibleVerb
U
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);
}
Unruh answered 14/7, 2012 at 20:20 Comment(0)
V
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.

Varus answered 29/9, 2012 at 22:49 Comment(0)
B
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.

Beira answered 6/6, 2011 at 19:59 Comment(2)
yes it does. probably you need to use BoxLayout.Y_AXIS to achieve that effect.Filemon
@Overbose could you elaborate? I was using BoxLayout.Y_AXIS in the example code.Beira

© 2022 - 2024 — McMap. All rights reserved.