Chapter 8. The Widgets

It is probably about time to start talking about the widgets that are available in Java-GNOME. There are so many widgets that I will not be able to cover them all. Instead I will just focus on a few of the most used and most complex. A good resource to learn about other widgets available in GTK is the GTK tutorial which is on the gtk web site. Although it discusses the C implimentation it is very useful for Java-GNOME developers since there are many similarities.

8.1. Gtk Entry

The Entry widget in GTK is a sinle line text entry widget. A fairly large set of key bindings are supported by default. If the entered text is longer than the allocation of the widget, the widget will scroll so that the cursor position is visible. It fires three EditableEvent events; changed, delete_text, and insert_text.

Below is a very brief example of how you can create an Entry widget.

Example 8-1. EntryDemo.java - Gtk Entry

import org.gnu.gtk.Entry;
import org.gnu.gtk.Gtk;
import org.gnu.gtk.HBox;
import org.gnu.gtk.Widget;
import org.gnu.gtk.Window;
import org.gnu.gtk.WindowType;
import org.gnu.gtk.event.LifeCycleEvent;
import org.gnu.gtk.event.LifeCycleListener;

public class EntryDemo {

	public static Widget createEntryWidget(){
		HBox b = new HBox(true,0);
		Entry entry = new Entry();
		entry.setText("Entry widget demo.");
		entry.setVisible(true);
		b.add(entry);
		return b;
	}
		
	public static void main(String[] args) {
		Gtk.init(args);
		Window w = new Window(WindowType.TOPLEVEL);
		w.addListener(new LifeCycleListener() {
			public void lifeCycleEvent(LifeCycleEvent event) {
				if (event.isOfType(LifeCycleEvent.Type.DESTROY) || 
					event.isOfType(LifeCycleEvent.Type.DELETE)) {
					Gtk.mainQuit();
				}
			}
		});
		w.setDefaultSize(200,30);
		w.setBorderWidth(5);
		w.setTitle("The Java-Gnome team");
		w.add(createEntryWidget());
		w.showAll();
		Gtk.main();
	}
}