In this chapter we will add menus to our application. But before we begin to look at menus let's restructure our code from the last chapter. Since Java is an object-oriented language we should structure our code to take advantage of this feature. Also, let's take our first look at signals, the event mechanism in GTK.
All GNOME and GTK applications are event driven. This means that when actions take place in the application it causes an event to be sent from a source and acted upon by a reciever. In fact, when we made the call Gtk.main() in the program in the last chapter we were telling our application to wait for an event and when the event arrives, a function is called. The event is called a signal in GNOME and GTK and the function is called a callback.
The example below demonstrates the concept of signals and callbacks. It is the example from the previous chapter (restructured a little) and adds callbacks to handle the delete_event and the destroy events.
Example 3-1. Second.java - first take
import gtk.*; import gnome.*; public class Second { private GnomeApp app = null; public static final String appVersion = "0.1"; public Second() { createMainWindow(); } 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(); } public boolean delete_event(int value) { return true; } public void destroy() { Gtk.mainQuit(); } public static void main(String[] args) { Gnome.init("Second", Second.appVersion, 0, 0); Second second = new Second(); Gtk.main(); } } |
First lets look at how we restructured the code. As you can see I have added two properties to our class. I have also added a constructor, a method to build the window, and two callback methods to handle the two events featured in this example.
The creation of the window is handled in the createMainWindow method which is called from the constructor. This method creates our GnomeApp, sets various pollicy and size information (covered in the previous chapter), connects the signals, and tells the GnomeApp to display itself.
Most window managers have a "Close" menu item on a menu on the toolbar. Selecting this menu option causes an event to be sent to your application. This event is the delete_event. Your application should intercept this event and perform any necessary cleanup. Things that you might want to do here is close files you have open, close any socket connections, prompt the user to save changed data, etc. The example above handles this by connecting the delete_event signal to the delete_event() method. This is performed with the following call:
Example 3-2.
app.signal_connect("delete_event", this); |
This call ties the delete_event event to a method in this object by the same name. Returning true from this method causes the application to send a destroy event. As you can see, we have connected the destroy event to a method with the same name with the following call:
Example 3-3.
app.signal_connect("destroy", this); |
It is in the destroy() method that we call the Gtk.mainQuit() method which causes the application to exit.
This has been a very brief introduction to signals and callbacks but since every example will be using them throughout the remainder of this tutorial we will stop here now and expand our discussions as we explore new topics. Now on to menus.