Tables

A Table is a layout manager that allows you to place you widgets in rows and columns. In an AWT world a Table would probably fall somewhere between a GridLayout and a GridBagLayout.

The constructor for the Table class is as follows:

public Table(int rows, int columns, boolean homogenous);

The first and second parameters specify the number of rows and columns. If you specify 2 rows and 2 columns you would get a table layout similar to the figure below. The final parameter specifies if all cells in the table should be the same size. If homogenous is set to true all cells are set to the size of the largest cell in the table.

      0             1              2
    0 +-------------+--------------+
      |             |              |
      |             |              |
    1 +-------------+--------------+
      |             |              |
      |             |              |
    2 +-------------+--------------+

Figure 1. A 2 X 2 table grid

The method to insert widgets into the table is as follows:

public void attach(Widget child, int left_attach, int right_attach, int top_attach, int bottom_attach, AttachOptions xoptions, AttachOptions yoptions, int xpadding, int ypadding);

On first appearances the method looks overwhelming. Like the Box layout managers, Table provides numerous methods that have default values for the parameters. Let's discuss the parameters for the attach() method and their default values.

The first parameter, child is the widget that is to be placed in the table.

The next four parameters, left_attach, right_attach, top_attach, and bottom_attach define the location of the widget within the grid. For example, to place a widget in the upper left corner of the grid shown above you would use 0, 1, 0, 1. To place a widget in the grid that would occupy the entire bottom row you would use 0, 2, 1, 2. These values are always required.

The next two parameters, xoptions and yoptions define how the widget places and sizes itself within the cell(s). They are of type AttachOptions and the possible values are described in the table below. You can provide multiple options by or'ing them together with the or() method of AttachOptions. The default values for these options is EXPAND and FILL.

Table 1. AttachOptions

ValueDescription
FILLThe widget expands to fill the cell(s) and grows as the table grows.
SHRINKIf the cell is smaller than the widget it contains minimum size then the widget can shrink to fit the available size.
EXPANDThis tells the table to expand to use any unused space around it.

The final two parameters, xpadding and ypadding, specify the number of pixels of padding to place on the sides of the widget. For example, if you set the value for xpadding to 5 for all of your widgets then there will be 10 pixels between them. 5 on the left and 5 on the right. The default values for the padding is 0.

As we stated before, there are many attach() methods for Table, each eliminating one or more of the parameters and utilizing the defaults. For a complete listing of these methods please refer to the javadoc for the Java-GNOME project.

Let's put what we have learned about the GNOME layout managers to use. In the next section we will expand on our example to demonstrate their use.