Adding menus to our example

There are many nice facilities available to GNOME applications for the creation of standardized menus. Unfortinately, most of those facilities are not available in Java-GNOME at this time. What is available is the GTK objects and we will explore them here.

Let's start by extending our example in this chapter to include menus.

Example 3-4. Second.java - second take


import gtk.*;
import gnome.*;

public class Second {
    private GnomeApp app = null;
    public static final String appVersion = "0.1";

    public Second() {
        createMainWindow();
        createMenus();
    }

    private void createMainWindow() {
        app = new GnomeApp("Second", "Second App");
        app.setPolicy(false, true, false);
        app.setWmclass("Second", "SecondApp");
        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 fileExit = new GtkMenuItem("Exit");
        fileExit.signal_connect("activate", "fileExit", this);
        file.setSubmenu(fileMenu);
        fileMenu.append(fileExit);
        // now display it
        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 it
        helpAbout.show();
        help.show();

        menubar.show();

        app.setMenus(menubar);
    }

    public boolean delete_event(int value) {
        return true;
    }

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

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

    public void helpAbout() {
        String title = "Second 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("Second", Second.appVersion, 0, 0);
        Second second = new Second();
        Gtk.main();
    }

}

Most of the work is done in the createMenus method which is called from the constructor. The first object created is of type GtkMenuBar. This object represents the menubar to goes across the top of our application.

The next two objects created are of type GtkMenuItem. These are visual objects and represent something that can be selected by the user. They are added to the menubar using the append() method. In our example we create two; one for the "File" menu and one for the "Help" menu.

Next we create a GtkMenu object for each item on the menubar. The GtkMenu object is a container for additional GtkMenuItems. This object is added to the GtkiMenuItem on the menubar with the setSubmenu() method.

Next we create GtkMenuItem objects for each menu item on a menu. These objects are added to their menu with the append() menu.

Finally, the menubar is added to the GnomeApp object with the setMenus() method. We have left out the fact that we must tell each visual object to show() itself.

I know this is very confusing so let me try to make it simpler here. For the "File" menu we first create a visual GtkMenuItem with "File" for its label. To this menu item we add a GtkMenu container to hold all menu items. We then add a GtkMenuItem with a label of "Exit" to the menu.

Handling Menu Events

When a GtkMenuItem is selected it emits an activate signal. We capture this event and perform a few tasks using a different version of the signal_connect() method. Let's look at that here.

Example 3-5.


fileExit.signal_connect("activate", "fileExit", this);

In this version, the first parameter is the name of the signal and the last parameter is the object that wishes to recieve the event like the pervious version to signal_connect(). The second parameter is the name of the method that is to be invoked when the event occurs.

We have mapped two GtkMenuItems activate event to methods in our example. "fileExit" is mapped to a method with the same name and we just call Gtk.mainQuit() to exit the application. "helpAbout" is mapped to a method with the same name and in this method we create a GnomeAbout object. This object is a popup window displaying information about the application.