Chapter 4. Toolbars and Statusbars

Table of Contents
Toolbars
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. As I said in the last chapter, many of these facilities are not currently available in Java-GNOME. Therefore, we will focus on the GTK toolbars. Also, GNOME applications us the standard GTK statusbar so we will also be looking at this here.

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. Often these buttons contain icons and text but since Java-GNOME doesn't have great support for icons at this time we will just create buttons with text. Let's get started by extending the example from the last chapter by adding a toolbar.

Example 4-1. Third.java - first take


import gtk.*;
import gnome.*;

public class Third {
    private GnomeApp app = null;
    private GtkToolbar toolbar = null;
    public static final String appVersion = "0.1";

    public Third() {
	createMainWindow();
	createMenus();
	createToolbar();
    }

    private void createMainWindow() {
	app = new GnomeApp("Third", "Third App");
	app.setPolicy(false, true, false);
	app.setWmclass("Third", "ThirdApp");
	app.setUsize(200, 200);
	app.signal_connect("delete_event", this);
	app.signal_connect("destroy", this);
	app.show();
    }

    private void createMenus() {
	GtkMenuBar menubar = new GtkMenuBar();
	GtkMenuItem file = new GtkMenuItem("File");
	GtkMenuItem help = new GtkMenuItem("Help");
	menubar.append(file);
	menubar.append(help);
	
	// finish the file menu
	GtkMenu fileMenu = new GtkMenu();
	GtkMenuItem fileNew = new GtkMenuItem("New");
	GtkMenuItem fileOpen = new GtkMenuItem("Open");
	GtkMenuItem fileSave = new GtkMenuItem("Save");
	GtkMenuItem fileSaveAs = new GtkMenuItem("Save As");
	GtkMenuItem fileExit = new GtkMenuItem("Exit");
	fileNew.signal_connect("activate", "fileNew", this);
	fileOpen.signal_connect("activate", "fileOpen", this);
	fileSave.signal_connect("activate", "fileSave", this);
	fileSaveAs.signal_connect("activate", "fileSaveAs", this);
	fileExit.signal_connect("activate", "fileExit", this);
	file.setSubmenu(fileMenu);
	fileMenu.append(fileNew);
	fileMenu.append(fileOpen);
	fileMenu.append(fileSave);
	fileMenu.append(fileSaveAs);
	fileMenu.append(fileExit);
	// now display it
	fileNew.show();
	fileOpen.show();
	fileSave.show();
	fileSaveAs.show();
	fileExit.show();
	file.show();
	
	// finish the help menu
	GtkMenu helpMenu = new GtkMenu();
	GtkMenuItem helpAbout = new GtkMenuItem("About");
	helpAbout.signal_connect("activate", "helpAbout", this);
	help.setSubmenu(helpMenu);
	helpMenu.append(helpAbout);
	// display
	helpAbout.show();
	help.show();
	
	menubar.show();
	
	app.setMenus(menubar);
    }

    private void createToolbar() {
	toolbar = new GtkToolbar(GtkOrientation.GTK_ORIENTATION_VERTICAL,
				 GtkToolbarStyle.GTK_TOOLBAR_TEXT);
	GtkButton button = new GtkButton("New");
	button.signal_connect("clicked", "fileNew", this);
	toolbar.appendWidget(button, "New File", "Private");
	button.show();
	button = new GtkButton("Open");
	button.signal_connect("clicked", "fileOpen", this);
	toolbar.appendWidget(button, "Open File", "Private");
	button.show();
	toolbar.appendSpace();
	button = new GtkButton("Save");
	button.signal_connect("clicked", "fileSave", this);
	toolbar.appendWidget(button, "Save File", "Private");
	button.show();
	button = new GtkButton("Save As");
	button.signal_connect("clicked", "fileSaveAs", this);
	toolbar.appendWidget(button, "Save File As", "Private");
	button.show();

	app.setToolbar(toolbar);
    }

    public boolean delete_event(int value) {
	return true;
    }
    
    public void destroy() {
	Gtk.mainQuit();
    }
    
    public void fileNew() {
        System.out.println("File New Selected");
    }

    public void fileOpen() {
        System.out.println("File Open Selected");
    }

    public void fileSave() {
        System.out.println("File Save Selected");
    }

    public void fileSaveAs() {
        System.out.println("File Save As Selected");
    }

    public void fileExit() {
	Gtk.mainQuit();
    }
    
    public void helpAbout() {
	String title = "Third App";
	String version = "Version " + appVersion;
	String license = "GPL";
	String[] authors = {"The Java-GNOME team"};
	String comments = "This example is a part of the Java-GNOME tutorial";
	String pixmap = "gftp.png";
	GnomeAbout about = new GnomeAbout(title, version, license,
					  authors, comments, pixmap);
	about.show();
    }
    
    public static void main(String[] args) {
	Gnome.init("Third", Third.appVersion, 0, 0);
	Third second = new Third();
	Gtk.main();
    }    
}

Ths first thing you will notice is that I have expanded our menus from the previous chapter. I will not review this code here. If you wish to review please read the previous chapter.

I added a member of type GtkToolbar named toolbar. This member is initialized in the createToolbar() method which is called from the constructor. The constructor for type GtkToolbar takes two parameters. The first is to set the orientation. The two possible values for this parameter are GtkOrientation.GTK_ORIENTATION_VERTICAL or GtkOrientation.GTK_ORIENTATION_HORIZONTAL. The second parameter to the constructor sets the style of the buttons to be displayed on the toolbar. The possible values are GtkToolbarStyle.GTK_TOOLBAR_TEXT, GtkToolbarStyle.GTK_TOOLBAR_ICON, or GtkToolbarStyle.GTK_TOOLBAR_BOTH. We have set our GtkToolbar to vertical and text.

Next we create a series of GtkButton widgets connecting their "clicked" signal to the appropriate callbacks. Then we add the buttons to the toolbar with the appendWidget()method. This method takes the widget as the first parameter, the tooltip text as the second parameter, and a private tooltip text as the third parameter.

All this is left is to add the toolbar to the GnomeApp by calling setToolbar(). As you can see, toolbars are very easy to use.