Chapter 4. Toolbars and Statusbars

In this chapter we will focus on adding a toolbar and statusbar to our application. GNOME uses many of the same facilities to create toolbars as it uses to create menus; namely UIInfo. This chapter will use the same classes from the previous chapter to create a toolbar and add it to the application. We sill also be creating a standard statusbar and connecting the "hints" from the UIInfo classes to the statusbar.

4.1. Toolbars

A Toolbar is a container used to group widgets in a dockable frame. Typically the toolbar contains buttons and is positioned across the top of the main window of an application. Let's get started by extending the example from the last chapter by adding a toolbar.

Example 4-1. Third.java - first take


import org.gnu.gnome.About;
import org.gnu.gnome.App;
import org.gnu.gnome.Program;
import org.gnu.gnome.GnomeStockItem;
import org.gnu.gnome.UIInfo;
import org.gnu.gtk.Gtk;
import org.gnu.gtk.StatusBar;
import org.gnu.gtk.event.ButtonEvent;
import org.gnu.gtk.event.ButtonListener;
import org.gnu.gtk.event.LifeCycleEvent;
import org.gnu.gtk.event.LifeCycleListener;
import org.gnu.gtk.event.MenuItemEvent;
import org.gnu.gtk.event.MenuItemListener;

public class Third implements MenuItemListener, ButtonListener {
	private App app = null;
	private StatusBar statusbar = null;
	public static final String appVersion = "0.1";

	public Third() {
		createMainWindow();
		createMenus();
		createToolbar();
		app.showAll();
	}

	private void createMainWindow() {
		app = new App("Third", "Third App");
		app.setDefaultSize(350, 200);
		app.addListener(new LifeCycleListener() {
			public void lifeCycleEvent(LifeCycleEvent event) {
				if (event.isOfType(LifeCycleEvent.Type.DESTROY) || 
					event.isOfType(LifeCycleEvent.Type.DELETE)) {
					Gtk.mainQuit();
				}
			}
		});
	}

	private void createMenus() {

		UIInfo fileMenu[] =
			{
				UIInfo.newItem("New Window", "Open a new application window", this),
				UIInfo.separator(),
				UIInfo.openItem((MenuItemListener) this),
				UIInfo.saveItem((MenuItemListener) this),
				UIInfo.saveAsItem((MenuItemListener) this),
				UIInfo.separator(),
				UIInfo.closeItem((MenuItemListener) this),
				UIInfo.quitItem(new MenuItemListener() { public void menuItemEvent(MenuItemEvent event) { fileExit();
				}
			}), UIInfo.end()
			};

		UIInfo editMenu[] =
			{
				UIInfo.undoItem((MenuItemListener) this),
				UIInfo.redoItem((MenuItemListener) this),
				UIInfo.separator(),
				UIInfo.cutItem((MenuItemListener) this),
				UIInfo.copyItem((MenuItemListener) this),
				UIInfo.pasteItem((MenuItemListener) this),
				UIInfo.separator(),
				UIInfo.findItem((MenuItemListener) this),
				UIInfo.findAgainItem((MenuItemListener) this),
				UIInfo.replaceItem((MenuItemListener) this),
				UIInfo.propertiesItem((MenuItemListener) this),
				UIInfo.end()};

		UIInfo moveMenu[] =
			{
				UIInfo.item("_Up", "Move selection up", (MenuItemListener) this),
				UIInfo.item("D_own", "Move selection down", (MenuItemListener) this),
				UIInfo.end()};

		UIInfo helpMenu[] =
			{
				UIInfo.help("second"),
				UIInfo.aboutItem(new MenuItemListener() { public void menuItemEvent(MenuItemEvent event) { helpAbout();
				}
			}), UIInfo.end()
			};

		UIInfo mainMenu[] =
			{
				UIInfo.subtree("_File", fileMenu),
				UIInfo.subtree("_Edit", editMenu),
				UIInfo.subtree("_Move", moveMenu),
				UIInfo.subtree("_Help", helpMenu),
				UIInfo.end()};

		app.createMenus(mainMenu);
	}

	private void createToolbar() {

		UIInfo toolbar[] =
			{
				UIInfo.itemStock("New", "Create a new file", (ButtonListener) this, GnomeStockItem.NEW),
				UIInfo.itemStock("Open", "Open a file", (ButtonListener) this, GnomeStockItem.OPEN),
				UIInfo.separator(),
				UIInfo.itemStock("Save", "Save this file", (ButtonListener) this, GnomeStockItem.SAVE),
				UIInfo.itemStock("Save As", "Save this file as", (ButtonListener) this, GnomeStockItem.SAVE_AS),
				UIInfo.separator(),
				UIInfo.itemStock("Close", "Close this file", (ButtonListener) this, GnomeStockItem.CLOSE),
				UIInfo.end()};

		app.createToolBar(toolbar);
	}

	public void helpAbout() {
		String title = "Third App";
		String version = "Version " + appVersion;
		String license = "GPL";
		String[] authors = { "The Java-GNOME team" };
		String[] documenters = { "" };
		String comments = "This example is a part of the Java-GNOME tutorial";
		About about = new About(title, version, license, comments, authors, documenters, "", null);
		about.show();
	}

	public void fileExit() {
		Gtk.mainQuit();
	}

	public void menuItemEvent(MenuItemEvent event) {
		app.message("Not implemented...");
	}

	public void buttonEvent(ButtonEvent event) {
	}

	public static void main(String[] args) {
		Program.initGnomeUI("Third", Third.appVersion, args);
		Third third = new Third();
		Gtk.main();
	}
}

In this example we have added a new method createToolbar(). This method creates a single array of UIInfo objects. As you can see, we are creating toolbar buttons with stock images with the itemStock() method.

Once we have created the array of UIInfo objects we create the toolbar with a call to createToolbar(). This causes a dockable toolbar to be created and attached to the top of the App window just below the menu.