Align

Align controls how a component is positioned inside the space allocated to it by a layout. It is particularly useful when cells have more width or height than their contents.

For the complete API reference, visit https://doc.jdance.org.

Overview

Horizontal alignment determines whether a component is placed at the start, center, or end of the available width. Alignment belongs to the layout cell, not to the text itself.

CODE
BasicLayout layout = new BasicLayout(BasicLayout.Direction.VERTICAL);
layout.setRelativeWidth(100);
layout.setGap(6);
layout.add(new Text("Left")).setHAlign(Align.LEFT).setBgColor(new Color("#DCEEFF")).setPadding(6);
layout.add(new Text("Center")).setHAlign(Align.CENTER).setBgColor(new Color("#E4F5E9")).setPadding(6);
layout.add(new Text("Right")).setHAlign(Align.RIGHT).setBgColor(new Color("#FFF0D8")).setPadding(6);
RESULT
Left
Center
Right

The preview above uses a small amount of space so the behavior is visible even in a narrow documentation panel.

Vertical placement

Vertical alignment matters when a cell has extra height. Combining horizontal and vertical alignment gives fine control without absolute positioning.

CODE
BasicLayout layout = new BasicLayout(BasicLayout.Direction.VERTICAL);
layout.setRelativeWidth(100);
layout.setHeight(120);
layout.setBackground(new Color("#F4F7FB"));
layout.add(new Text("Centered")).setVAlign(Align.CENTER).setHAlign(Align.CENTER);
RESULT
Centered

Alignment and responsive design

A centered component is still subject to its containing layout. If the layout becomes narrower, the available positioning space also changes. Use alignment for placement and padding or gap for spacing.

Aligning one cell

The same idea can also be expressed as a compact Java snippet without requiring a live preview.

CODE
layout.add(new Text("Center"))
        .setHAlign(Align.CENTER)
        .setVAlign(Align.CENTER);

Practical guidance

A common mistake is to treat alignment as a substitute for sizing. Give the containing layout enough width or height before expecting LEFT, CENTER or RIGHT to produce visibly different positions.

Full API reference

These examples focus on the day-to-day use of Align. The full API documentation contains additional options, inherited properties and complete method signatures.

Visit https://doc.jdance.org for the full reference.