2

I have Hbox with labels inside. This box sometimes is smaller, sometimes is bigger. Is there any way to force it's children (labels) to resize like: label1 resizes first, if it can't be smaller then label2 resizes, if it can't be smaller label3 resizes etc.?

MrKaszu
  • 71
  • 8

1 Answers1

3

No, there are only 3 different resizing behaviors.

  • NEVER
  • SOMETIMES and
  • ALWAYS

NEVER is obviously not what you need and you cannot make the 3 children in 3 different ways with the remaining 2 resizing priorities.

You need to implement this kind of layout yourself:

public class HLayout extends Pane {

    @Override
    protected void layoutChildren() {
        final double w = getWidth();
        final double h = getHeight();
        final double baselineOffset = getBaselineOffset();

        List<Node> managedChildren = getManagedChildren();
        int size = managedChildren.size();

        // compute minimal offsets from the left and the sum of prefered widths
        double[] minLeft = new double[size];
        double pW = 0;
        double s = 0;
        for (int i = 0; i < size; i++) {
            minLeft[i] = s;
            Node child = managedChildren.get(i);
            s += child.minWidth(h);
            pW += child.prefWidth(h);
        }

        int i = size - 1;
        double rightBound = Math.min(w, pW);
        // use prefered sizes until constraint is reached
        for (; i >= 0; i--) {
            Node child = managedChildren.get(i);
            double prefWidth = child.prefWidth(h);
            double prefLeft = rightBound - prefWidth;
            if (prefLeft >= minLeft[i]) {
                layoutInArea(child, prefLeft, 0, prefWidth, h, baselineOffset, HPos.LEFT, VPos.TOP);
                rightBound = prefLeft;
            } else {
                break;
            }
        }
        // use sizes determined by constraints
        for (; i >= 0; i--) {
            double left = minLeft[i];
            layoutInArea(managedChildren.get(i), left, 0, rightBound-left, h, baselineOffset, HPos.LEFT, VPos.TOP);
            rightBound = left;
        }
    }

}

Note that you should probably also override the implementation of computing the pref sizes.

Example use:

@Override
public void start(Stage primaryStage) {
    HLayout hLayout = new HLayout();

    // fills space required for window "buttons"
    Region filler = new Region();
    filler.setMinWidth(100);
    filler.setPrefWidth(100);

    Label l1 = new Label("Hello world!");
    Label l2 = new Label("I am your father!");
    Label l3 = new Label("To be or not to be...");
    hLayout.getChildren().addAll(filler, l1, l2, l3);

    Scene scene = new Scene(hLayout);

    primaryStage.setScene(scene);
    primaryStage.show();
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • great, but how to change It's aligment to left? I did it by simply iterate from 0 to size-1 but now every label instead of last is shorten to "...", doesn't metter what's HLayout width. How to fix it? – MrKaszu Feb 24 '17 at 15:47
  • @MrKaszu This requires you to limit the initial `rightBound` by the sum of the prefered widths of the children. I edited the answer. – fabian Feb 24 '17 at 16:05