Aligning components in Box

1k views Asked by At

edit: if you downvote this question, you may leave a comment to explain why, that will be more constructive.

I obtain this unexpected result...

enter image description here

... using this code:

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestAlignment extends JFrame {

    // Constructor
    public TestAlignment() {

        super("Test Alignment");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Components to show
        JLabel leftLabel = new JLabel("Left");
        JButton centerButton = new JButton("Middle");
        JLabel rightLabel = new JLabel("Right");

        // Add all to box
        Box box = Box.createVerticalBox();
        box.add(leftLabel);
        box.add(centerButton);
        box.add(rightLabel);
        box.add(Box.createHorizontalStrut(180));

        // Align content in box
        leftLabel.setAlignmentX(LEFT_ALIGNMENT);
        centerButton.setAlignmentX(CENTER_ALIGNMENT);
        rightLabel.setAlignmentX(RIGHT_ALIGNMENT);

        // Add box to frame, and show frame
        box.setOpaque(true);
        setContentPane(box);
        setVisible(true);
    }

    // Main
    public static void main(String[] args) {
        // Create frame in EDT
        SwingUtilities.invokeLater(new Runnable() {         
            @Override public void run() { new TestAlignment(); }
        });
    }
}

I understand now this works as expected for JComponent.setAlignmentX(): this method tells which sides of the components must be aligned (top label leftmost side aligned with button center and bottom label rightmost side).

I would like to understand how to have each label aligned as expected intuitively (left label on the left, right label on the right), labels touching the vertical edges of the Box?

(I know how to do with putting each label in a Box embedded, and using Box.createHorizontalGlue() to force it to the left or the right side, but seems to me too much for the simple purpose of alignment. I'm looking for something more simple)

2

There are 2 answers

1
chenop On

You can use BoxLyout:

        // Components to show
    // Left
    JLabel leftLabel = new JLabel("Left");
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
    leftPanel.add(leftLabel);
    leftPanel.add(Box.createHorizontalGlue());

    // Center
    JButton centerButton = new JButton("Middle");

    // Right
    JLabel rightLabel = new JLabel("Right");
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.X_AXIS));
    rightPanel.add(Box.createHorizontalGlue());
    rightPanel.add(rightLabel);

    // Add all to box
    Box box = Box.createVerticalBox();
    box.add(leftPanel);
    box.add(centerButton);
    box.add(rightPanel);
    box.add(Box.createHorizontalStrut(180));

    // Align content in box
    // leftLabel.setAlignmentX(LEFT_ALIGNMENT);
    centerButton.setAlignmentX(CENTER_ALIGNMENT);
    // rightLabel.setAlignmentX(RIGHT_ALIGNMENT);

enter image description here

0
camickr On

Don't think you can do this with a BoxLayout. Your example does show the intuitive code, which doesn't work as you would hope.

I would suggest you probably need to use a GridBagLayout. I think it supports the setAlignmentX(...) method the way you want to use it.

If not, then can use the Relative Layout. It is simple to use, like the BoxLayout and does support the alignment you want when you use:

setAlignment( RelativeLayout.COMPONENT );