Java-GNOME 2.10 GNOME Tutorial
<<< Previous |  | Next >>>

# Layout Management

This chapter discusses placing widgets on windows. The positioning is accomplished with the help of layout managers. In GNOME this is called packing widgets. GTK provides two such layout managers, a table and a box, and we will discuss both in this chapter. This chapter will not discuss the need for layout managers as most Java developers already use them for GUI development.

# Boxes

Those of you who have used the JDK's Box layout manager shoud be familiar with the concept of the GTK boxes. They layout their children either side by side (the horizontal box) or one on top of the other (the vertical box). That is where their similarities end. The GNOME boxes offer more flexibility in the way widgets behave once they are under the control of a box.

VBox and HBox are the two box classes that provide vertical and horizontal layout. The constructors for these classes are as follows:

`public `VBox``(boolean `homogenous`, int `spacing`);

`public `HBox``(boolean `homogenous`, int `spacing`);

The first parameter to the constructor, boolean homogenous, tells the VBox if all children should occupy the same height and tells the HBox if all children should occupy the same width. The second parameter, int spacing is the number of pixels of space to place around every child widget in the box. If you specify 4 for this parameter it will place 8 pixels between each widget due to the fact that it places this space on each side of the widget.

Once a box is created, widgets can be added with the following calls:

`public void `packStart``(GtkWidget `child`, boolean `expand`, boolean `fill`, int `padding`);

`public void `packStart``(Widget `child`, boolean `expand`, boolean `fill`);

`public void `packStart``(GtkWidget `child`, boolean `expand`);

`public void `packStart``(Widget `child`);

`public void `packEnd``(Widget `child`, boolean `expand`, boolean `fill`, int `padding`);

`public void `packEnd``(Widget `child`, boolean `expand`, boolean `fill`);

`public void `packEnd``(Widget `child`, boolean `expand`);

`public void `packEnd``(Widget `child`);

`public void `packStartDefaults``(Widget `child`);

`public void `packEndDefaults``(Widget `child`);

This seems like a lot but it is actually very simple. The packStart() series of methods pack widgets starting at the beginning of the box. The packEnd() series of methods pack widgets starting at the end of the box. The parameters tell the box how to layout the child in its' position. As the methods eliminate one or more of the parameters a default value is used. The next few paragraphs discuss the parameters to the methods and what impact they have on the child widget.

The expand parameter causes the children to use the entire box. This parameter doesn't affect that actual size of the widget, but it does center the widget in a region whose size is determined by the homogenous parameter, the number of widgets, and the size of the box. If the homogenous parameter is true, each widget gets the same area. Otherwise, the area is determined on a percentage basis which is based on the widget's minimum size. The default value for the methods that do not include this paramater is true.

The fill parameter instructs the children to grow to fill all of the area that is allocated to them. By default a widgets size is it's minimum size (width for horizontal boxes, and height for vertical boxes) but the fill parameter will cause it to expand until it fills it's entire area (either height or width depending on the type of box). The default value for the methods that do not include this parameter is true.

The padding parameter places additional pixels on both sides of the child. For vertical boxes, it places a space that is padding pixes in size on the top and bottom of a child. For horizontal boxes, it places a space on the left and right. The default value for this parameter is 0.

One thing that should be noted is that if you set the homogenous value in the constructor to true the expand parameter has no effect. As you recall, the homogenous parameter tells the control to space all widgets the same size.

That is about all there is to boxes. It seems very simple and you might be asking how can you achieve an advanced layout with such a simple layout manager. Many GNOME applications position their widgets by placing boxes within boxes to achieve a high degree of flexibility in their layout.

<<< Previous | Home | Next >>>
Statusbars |  | Tables