Skip to content
Advertisement

Misaligned box in box layout when adding JScrollpane

I wanted to create a simple table with the some of the features of a JTable, but easier to style, so I created a couple of boxes in a box layout, one for the header and another to contain the cells. When I added the JScrollPane to the cell’s box, it indented the header box and now the don’t line up.

Misaligned box:

https://i.stack.imgur.com/hKGk7.png

I’ve tried setAlignmentX and ComponentOrientation, but not luck. Does anyone have any ideas?

public class GUIInventory extends JPanel
{
    Box headersBox = Box.createHorizontalBox();
    Box cellsBox = Box.createVerticalBox();
    JScrollPane scrollPane;
    ArrayList<Box> rows = new ArrayList<>();
    ArrayList<JLabel> cells = new ArrayList<>();
    JLabel[] headerLabels = new JLabel[4];
    String[] headerLabelNames = {"Name", "Order Date", "Order Number", "Cost"};

    public GUIInventory()
    {
        this.setBackground(Color.WHITE);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setAlignmentX(Component.LEFT_ALIGNMENT);

        Border outer = BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY);
        Border inner = BorderFactory.createEmptyBorder(10, 10, 10, 10);

        for(int i = 0; i < this.headerLabels.length; i++)
        {
            this.headerLabels[i] = new JLabel(this.headerLabelNames[i]);
            this.headerLabels[i].setBorder(BorderFactory.createCompoundBorder(outer, inner));
            this.headerLabels[i].setFont(new Font("Arial", Font.BOLD, 16));
            this.headerLabels[i].setMaximumSize(new Dimension(200, (int) this.headerLabels[i].getPreferredSize().getHeight()));
            this.headerLabels[i].setAlignmentX(Component.LEFT_ALIGNMENT);
            this.headersBox.add(this.headerLabels[i]);
        }


        this.headersBox.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 1, 0, 0, Color.LIGHT_GRAY), BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLACK)));
        this.headersBox.setAlignmentX(Component.LEFT_ALIGNMENT);
        this.headersBox.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));

        this.cellsBox.setBorder(BorderFactory.createMatteBorder(1, 1, 0, 0, Color.LIGHT_GRAY));
        this.cellsBox.setAlignmentX(Component.LEFT_ALIGNMENT);

        this.scrollPane = new JScrollPane(this.cellsBox);

        this.add(this.headersBox);
        this.add(this.scrollPane);
    }

Advertisement

Answer

I’ve tried setAlignmentX

That is only relevant for the components that you add to the panel using the vertical BoxLayout:

this.add(this.headersBox);
this.add(this.scrollPane);

You set the alignment on the headersBox, but not the scrollPane. So you are missing:

this.scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);

Also

this.setAlignmentX(Component.LEFT_ALIGNMENT);

is not needed, since this is the parent panel, not a child component.

and,

this.headerLabels[i].setAlignmentX(Component.LEFT_ALIGNMENT);

is not needed, since left alignment is not relevant for a horizontal layout.

and,

this.cellsBox.setAlignmentX(Component.LEFT_ALIGNMENT);

is not needed since this component is added to the scroll pane

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement