This is the third part of a tutorial series about layout managers, container objects and object hierarchies in Qooxdoo. Read the first part here.
qx.ui.layout.HBox layout manager is similar to VBox but it places its children side by side.

HBox Layout
Let us begin with a simple application:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button("Child Widget 1"));
17 main_container.add(new qx.ui.form.Button("Child Widget 2"));
18 main_container.add(new qx.ui.form.Button("Child Widget 3"));
19
20 var application_root = this.getRoot();
21 application_root.add(main_container);
22 }
23 }
24 });

HBox Example 1
Ok, nothing special here. In the next example we will see how we can apply different constraints to individual child elements:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button(
17 "Child Widget 1").set({width:300}));
18 main_container.add(new qx.ui.form.Button("Child Widget 2"));
19 main_container.add(new qx.ui.form.Button(
20 "Child Widget 3").set({width:75}));
21 main_container.add(new qx.ui.form.Button(
22 "Child Widget 4 has a slightly longer title"));
23
24 var application_root = this.getRoot();
25 application_root.add(main_container);
26 }
27 }
28 });

HBox Example 2
We have set some properties on child widgets here. I know, it’s a bit ugly doing it on the same line with the addition. But screen space is more expensive now on the intertubes since the economic crisis1. The important point here is we have set width of first and third widgets explicitly. Second and fourth will be automatically sized. Also notice that the fourth widget is wider. Buttons consider length of their labels when calculating their sizes.
The container will also be auto-sized on creation. But it won’t fill the viewport since we haven’t coded this behaviour (see previous part). Next example will include this behaviour:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button("Child Widget 1").set({
17 width: 300,
18 allowGrowX: true,
19 }), {flex: 1});
20 main_container.add(new qx.ui.form.Button("Child Widget 2").set({
21 minWidth: 75,
22 maxWidth: 200,
23 }), {flex: 1});
24 main_container.add(new qx.ui.form.Button("Child Widget 3").set({
25 width: 75,
26 allowGrowX: false,
27 }), {flex: 1});
28
29 var application_root = this.getRoot();
30 application_root.add(main_container);
31
32 application_root.addListener("resize", function(e) {
33 main_container.set({
34 "width": qx.bom.Viewport.getWidth(),
35 });
36 }, this);
37 }
38 }
39 });

HBox Example 3
We have quite a few changes this time. First of all each widget is set to flex. First and third widgets have their (different) widths explicitly set. Growing is enabled for the first2 and disabled for the third. For the second widget we have created different constraints; we have set the maximum and minimum values for width.
Try resizing the window to see how each widget reacts. We should note that the second widget never goes beyond its limits. While the other two conform to fit the containers space. MinWidth and MaxWidth are stronger constraints than width.
Let’s try constraining the container’s size:
1 /*
2 #asset(hbox/ *)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15 main_container.set({
16 maxWidth: 900,
17 minWidth: 400,
18 });
19
20 main_container.add(new qx.ui.form.Button("Child Widget 1").set({
21 width: 300,
22 allowGrowX: true,
23 }), {flex: 1});
24 main_container.add(new qx.ui.form.Button("Child Widget 2").set({
25 minWidth: 75,
26 maxWidth: 200,
27 }), {flex: 1});
28 main_container.add(new qx.ui.form.Button("Child Widget 3").set({
29 width: 75,
30 allowGrowX: false,
31 }), {flex: 1});
32
33 var application_root = this.getRoot();
34 application_root.add(main_container);
35
36 application_root.addListener("resize", function(e) {
37 main_container.set({
38 "width": qx.bom.Viewport.getWidth(),
39 });
40 }, this);
41 }
42 }
43 });
We have set the maximum and minimum width for the container widget. If you resize the window you should notice the container obeying these constraints.
VBox & HBox Wrap-up
VBox and HBox are simple layout managers. I have mostly tried to illustrate how container preferences, layout preferences and child widget preferences work together with examples. In the next chapters we’ll work on more advanced layout managers and then composite layouts with multiple containers.
NEXT PART: Grid Layout
1: This is a lame joke, I’ve just done that for the sake of brevity.
2: This is redundant. Grow and Shrink properties are true by default.
No related posts.
This is the third part of a tutorial series about layout managers, container objects and object hierarchies in Qooxdoo. Read the first part [here](/2009/01/using-layouts-in-qooxdoo-part-1/).
`qx.ui.layout.HBox` layout manager is similar to `VBox` but it places its children side by side.
[caption id="attachment_128" align="aligncenter" width="300" caption="HBox Layout"][/caption]
Let us begin with a simple application:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button("Child Widget 1"));
17 main_container.add(new qx.ui.form.Button("Child Widget 2"));
18 main_container.add(new qx.ui.form.Button("Child Widget 3"));
19
20 var application_root = this.getRoot();
21 application_root.add(main_container);
22 }
23 }
24 });
[caption id="attachment_129" align="aligncenter" width="300" caption="HBox Example 1"][/caption]
Ok, nothing special here. In the next example we will see how we can apply different constraints to individual child elements:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button(
17 "Child Widget 1").set({width:300}));
18 main_container.add(new qx.ui.form.Button("Child Widget 2"));
19 main_container.add(new qx.ui.form.Button(
20 "Child Widget 3").set({width:75}));
21 main_container.add(new qx.ui.form.Button(
22 "Child Widget 4 has a slightly longer title"));
23
24 var application_root = this.getRoot();
25 application_root.add(main_container);
26 }
27 }
28 });
[caption id="attachment_130" align="aligncenter" width="300" caption="HBox Example 2"][/caption]
We have set some properties on child widgets here. I know, it's a bit ugly doing it on the same line with the `add`ition. But screen space is more expensive now on the intertubes since the economic crisis1. The important point here is _we have set width of first and third widgets explicitly_. Second and fourth will be automatically sized. Also notice that the fourth widget is wider. Buttons consider length of their labels when calculating their sizes.
The container will also be auto-sized on creation. But it won't fill the viewport since we haven't coded this behaviour (see [previous part](/2009/02/using-layouts-in-qooxdoo-part-2-vbox-layout/)\). Next example will include this behaviour:
1 /*
2 #asset(hbox/*)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15
16 main_container.add(new qx.ui.form.Button("Child Widget 1").set({
17 width: 300,
18 allowGrowX: true,
19 }), {flex: 1});
20 main_container.add(new qx.ui.form.Button("Child Widget 2").set({
21 minWidth: 75,
22 maxWidth: 200,
23 }), {flex: 1});
24 main_container.add(new qx.ui.form.Button("Child Widget 3").set({
25 width: 75,
26 allowGrowX: false,
27 }), {flex: 1});
28
29 var application_root = this.getRoot();
30 application_root.add(main_container);
31
32 application_root.addListener("resize", function(e) {
33 main_container.set({
34 "width": qx.bom.Viewport.getWidth(),
35 });
36 }, this);
37 }
38 }
39 });
[caption id="attachment_131" align="aligncenter" width="300" caption="HBox Example 3"][/caption]
We have quite a few changes this time. First of all each widget is set to `flex`. First and third widgets have their (different) `width`s explicitly set. `Grow`ing is enabled for the first2 and disabled for the third. For the second widget we have created different constraints; we have set the maximum and minimum values for `width`.
Try resizing the window to see how each widget reacts. We should note that the second widget never goes beyond its limits. While the other two conform to fit the containers space. `MinWidth` and `MaxWidth` are stronger constraints than `width`.
Let's try constraining the container's size:
1 /*
2 #asset(hbox/ *)
3 */
4 qx.Class.define("hbox.Application",
5 {
6 extend: qx.application.Standalone,
7 members: {
8 main: function(){
9 this.base(arguments);
10
11 var main_container = new qx.ui.container.Composite();
12
13 var layout_manager = new qx.ui.layout.HBox();
14 main_container.setLayout(layout_manager);
15 main_container.set({
16 maxWidth: 900,
17 minWidth: 400,
18 });
19
20 main_container.add(new qx.ui.form.Button("Child Widget 1").set({
21 width: 300,
22 allowGrowX: true,
23 }), {flex: 1});
24 main_container.add(new qx.ui.form.Button("Child Widget 2").set({
25 minWidth: 75,
26 maxWidth: 200,
27 }), {flex: 1});
28 main_container.add(new qx.ui.form.Button("Child Widget 3").set({
29 width: 75,
30 allowGrowX: false,
31 }), {flex: 1});
32
33 var application_root = this.getRoot();
34 application_root.add(main_container);
35
36 application_root.addListener("resize", function(e) {
37 main_container.set({
38 "width": qx.bom.Viewport.getWidth(),
39 });
40 }, this);
41 }
42 }
43 });
We have set the maximum and minimum `width` for the container widget. If you resize the window you should notice the container obeying these constraints.
### VBox & HBox Wrap-up
`VBox` and `HBox` are simple layout managers. I have mostly tried to illustrate how container preferences, layout preferences and child widget preferences work together with examples. In the next chapters we'll work on more advanced layout managers and then composite layouts with multiple containers.
NEXT PART: [Grid Layout](/2009/02/using-layouts-in-qooxdoo-part-4-grid-layout/)
-----
**1:** This is a lame joke, I've just done that for the sake of brevity.
**2:** This is redundant. `Grow` and `Shrink` properties are `true` by default.
Tags: hbox, javascript, layout, qooxdoo, ria, widget
This entry was posted
on Sunday, February 15th, 2009 at 14:00 and is filed under Games, Programming.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.