3

Here's a JFrame which I intended to show with a series of JLabels with the following properties:

  • stacked vertically
  • centered horizontally
  • green border
  • white background
  • blue text

But I get this instead:

enter image description here

The blue text, stacked vertically, green border work OK but the white background and centered horizontally do not. I also would have thought the labels would span the entire width of the JPanel.

What am I doing wrong?


edit: Missed this question about background color. So my remaining question is about BoxLayout and the positioning of components in the other axis.


import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class BoxLayoutLabelsTest extends JFrame
{
    public BoxLayoutLabelsTest(String title)
    {
        super(title);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        addLabel(panel, "Hydrogen");
        addLabel(panel, "Helium");
        addLabel(panel, "Lithium");
        addLabel(panel, "Beryllium");
        addLabel(panel, "Boron");

        setContentPane(panel);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    static private void addLabel(JPanel panel, String text) {
        JLabel label = new JLabel(text);
        label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        label.setBackground(Color.WHITE);
        label.setForeground(Color.BLUE);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label);
    }

    public static void main(String[] args) {
        new BoxLayoutLabelsTest("BoxLayoutLabelsTest").setVisible(true);
    }

}
Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

4

Add the following line into addLabel():

label.setAlignmentX(CENTER_ALIGNMENT);

See How To Use BoxLayout for complete example.

Added later:

I've found straightforward solution:

label.setMaximumSize(new Dimension(200, 200));
//label.setAlignmentX(CENTER_ALIGNMENT);//aligns label itself
label.setHorizontalAlignment(SwingConstants.CENTER);//aligns text inside the label

This also works, but your solution with BorderLayout seems more appropriate.

MockerTim
  • 2,475
  • 1
  • 25
  • 31
  • OK, that gets me centering... thanks. Is there any way to make the JLabel expand to the full width of the panel? Or is that one of those things that just aren't done this way? – Jason S Jun 09 '11 at 19:48
  • @Jason S For expansion you probably chosen wrong layout. It seems that it's layout task to make the expansion, and BoxLayout isn't able to expand single component. – MockerTim Jun 09 '11 at 20:28
  • 1
    I wrapped each of the JLabels in a JPanel with BorderLayout and BorderLayout.CENTER placement. That seems to work. – Jason S Jun 09 '11 at 20:30
  • I was using miglayout and nothing in that worked for just something as simple as this. Thanks saved me some time! +1'd – JPM Mar 23 '17 at 02:40