Aligning components in Box
Asked Answered
G

2

0

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)

Gaiter answered 19/11, 2014 at 19:22 Comment(1)
If you had spent a moment reading the helpful tag pop-ups, you'd have realized this has nothing to do with what the box tag was defined for..Tenpin
S
1

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 );
Snips answered 19/11, 2014 at 19:42 Comment(0)
P
1

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

Platitudinize answered 19/11, 2014 at 20:49 Comment(1)
I already knew this was possible using glue. As stated I'm looking for something more simple, if it exists. Anyway thanks for the answer Chen.Gaiter

© 2022 - 2024 — McMap. All rights reserved.