Chapter 6. Containers

There are widgets in GTK and GNOME whose sole purpose is to contain other widgets. This chapter will discuss a few of these widgets.

6.1. Alignment

The Alignment container is designed to hold a single widget and control the widgets size and position. The widget is positioned in the container a certain percentage of the distance from the top and left sides of the container. The widget is sized a certain percentage of the containers height and width. Let's look at a small example to demonstrate it usage.

Example 6-1. Alignment.java - Alignment

import org.gnu.gtk.Alignment;
import org.gnu.gtk.Button;
import org.gnu.gtk.Gtk;
import org.gnu.gtk.Window;
import org.gnu.gtk.WindowType;
import org.gnu.gtk.event.LifeCycleEvent;
import org.gnu.gtk.event.LifeCycleListener;

public class AlignmentExample {

	public AlignmentExample() {

		Window window = new Window(WindowType.TOPLEVEL);
		window.setTitle("Alignment Example");
		window.addListener(new LifeCycleListener() {
			public void lifeCycleEvent(LifeCycleEvent event) {
				if (event.isOfType(LifeCycleEvent.Type.DESTROY) || 
					event.isOfType(LifeCycleEvent.Type.DELETE)) {
					Gtk.mainQuit();
				}
			}
		});
		window.setDefaultSize(100, 100);

		Button button = new Button("The Button Label", false);
		// We will create an Alignment and add the button to it.
		// The x alignment is set to 1.0, which keeps the button 
		// against the right edge.  The y alignment is 0.5, keeping
		// the widget vertically centered.  The scale value is set
		// to 0.15 in both directions, which limits the expansion
		// of the button to no more than 15 percent of the area 
		// made available to it.
		Alignment alignment = new Alignment(1.0, 0.5, 0.15, 0.15);
		alignment.add(button);
		window.add(alignment);

		window.showAll();
	}

	public static void main(String[] args) {
		// Initialize GTK 
		Gtk.init(args);

		AlignmentExample align = new AlignmentExample();

		Gtk.main();
	}
}