From 5b4b93133d51d3c0ccf31d8ecb41505c7d2a45f9 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Wed, 8 Feb 2017 19:05:31 +0200 Subject: Add addComponentsAndExpand to horizontal and vertical layout (#8480) --- .../main/java/com/vaadin/ui/HorizontalLayout.java | 22 ++++++++++++++++++++++ .../main/java/com/vaadin/ui/VerticalLayout.java | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'server/src/main') diff --git a/server/src/main/java/com/vaadin/ui/HorizontalLayout.java b/server/src/main/java/com/vaadin/ui/HorizontalLayout.java index 07583c6712..ccee900ca1 100644 --- a/server/src/main/java/com/vaadin/ui/HorizontalLayout.java +++ b/server/src/main/java/com/vaadin/ui/HorizontalLayout.java @@ -60,4 +60,26 @@ public class HorizontalLayout extends AbstractOrderedLayout { return (HorizontalLayoutState) super.getState(markAsDirty); } + /** + * Adds the given components to this layout and sets them as expanded. The + * width of all added child components are set to 100% so that the expansion + * will be effective. The width of this layout is also set to 100% if it is + * currently undefined. + * + * @param components + * the components to set, not null + */ + public void addComponentsAndExpand(Component... components) { + addComponents(components); + + if (getWidth() < 0) { + setWidth(100, Unit.PERCENTAGE); + } + + for (Component child : components) { + child.setWidth(100, Unit.PERCENTAGE); + setExpandRatio(child, 1); + } + } + } diff --git a/server/src/main/java/com/vaadin/ui/VerticalLayout.java b/server/src/main/java/com/vaadin/ui/VerticalLayout.java index d441e8a664..5d8bda3b4c 100644 --- a/server/src/main/java/com/vaadin/ui/VerticalLayout.java +++ b/server/src/main/java/com/vaadin/ui/VerticalLayout.java @@ -62,4 +62,26 @@ public class VerticalLayout extends AbstractOrderedLayout { protected VerticalLayoutState getState(boolean markAsDirty) { return (VerticalLayoutState) super.getState(markAsDirty); } + + /** + * Adds the given components to this layout and sets them as expanded. The + * height of all added child components are set to 100% so that the + * expansion will be effective. The height of this layout is also set to + * 100% if it is currently undefined. + * + * @param components + * the components to set, not null + */ + public void addComponentsAndExpand(Component... components) { + addComponents(components); + + if (getHeight() < 0) { + setHeight(100, Unit.PERCENTAGE); + } + + for (Component child : components) { + child.setHeight(100, Unit.PERCENTAGE); + setExpandRatio(child, 1); + } + } } -- cgit v1.2.3