BoxLayout adding left margin

1.9k views Asked by At

I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.

enter image description here

My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.

enter image description here

Here's my code, the second image is created by not adding headerPanel:

JLabel commandLabel = new JLabel(command);
    JLabel paramLabel = new JLabel(params);
    JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
    Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;

    commandFont = baseFont.deriveFont(Font.BOLD);
    paramFont = baseFont.deriveFont(Font.ITALIC);
    descFont = baseFont.deriveFont(Font.PLAIN);

    commandLabel.setFont(commandFont);
    paramLabel.setFont(paramFont);
    descLabel.setFont(descFont);
    descLabel.setAlignmentX(LEFT_ALIGNMENT);
    descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));   
    JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        headerPanel.add(commandLabel);
        headerPanel.add(paramLabel);
    this.add(headerPanel);
    this.add(descLabel);

This class extends JPanel, and is added to a JFrame, which is simply pack()'d

2

There are 2 answers

1
Arnaud On BEST ANSWER

Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :

    JLabel commandLabel = new JLabel(command);
    JLabel paramLabel = new JLabel(params);
    JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
    Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;

    commandFont = baseFont.deriveFont(Font.BOLD);
    paramFont = baseFont.deriveFont(Font.ITALIC);
    descFont = baseFont.deriveFont(Font.PLAIN);

    commandLabel.setFont(commandFont);
    paramLabel.setFont(paramFont);
    descLabel.setFont(descFont);
    descLabel.setAlignmentX(LEFT_ALIGNMENT);
    descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));   
    JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
    headerPanel.add(commandLabel);
    headerPanel.add(paramLabel);

    descPanel.add(descLabel);// added

    this.add(headerPanel);
    this.add(descPanel);// modified
0
camickr On

My problem is the margin to the left of the large lipsum box, how can I get rid of this?

You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.

I'm guessing the JLabel is center aligned so you need to use:

descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);

See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.