Presentations in Java programs

In this section you will learn how to use a presentation in your Java program. In this way you can develop a user interface using Bongo and do the actions in your Java program. The example called username in the subdirectory demo/programming will be used to illustrate this.


Basics

In the marimba.gui package a class PlayerFrame has been defined; it displays presentations for you and has some shortcuts for accessing widgets from that presentation using the PlayerUtil. In order to be able to use a presentation you will have to extend the class PlayerFrame. Using the method setPresentation in 'util' you can set the presentation to the correct presentation file. After that you can decide to show the presentation by calling util.show(). And by using the handleEvent method you can handle Events from the presentation.

To illustrate all this we will now go through the username example.


The username presentation

First of all we'll start with the user interface as you can see below.

The two labels are textboxes with the style "plain" and are not editable. The textbox in which the name can be entered has all the default values for the properties. The two buttons also have the default properties, except for the Ok button that is set to be the default button: when the user presses return in the textbox, this button is executed. The font can be made larger by setting the font of the presentation: in the example it has been set to 18 points.
To set the font of the presentation choose Edit - Select Presentation which has as result that the black-yellow dashed line is drawn around the presentation and no widget is selected, like in the image above. After that you can set the point size of the font to 18. We also want to display the background that you can see in the image: set the fillmode of the presentation to pattern and set the pattern to "~builder/rain.gif". "~builder" refers to the bongo directory that contains all its images and presentations, we are using its rain pattern. Don't forget to hit apply after you did all this.

Now the user interface is ready. We must be able to get the two buttons and the textbox for the name. Select one of the widgets and give it the correct name. The names are: okButton, cancelButton and userName. The non-editable textbox that contains "Please enter your name" should be named message. After you did this the user interface is done and you can save it. Save it under the name "username" in a new empty directory.


The username application

What we have to do next is create a class that extends PlayerFrame. The heading of the class would look like this:

Always make sure to import everything from the marimba.gui package in order to be able to acces anything within that package.

In this new class username we will override handleEvent in order to do the following: when the user executes the ok button the application checks whether a name has been entered: if no name was entered it displays a warning message otherwise it prints the given name to the console and exit. If the user presses the cancel button the application will print "Cancelled" to the console and will exit.

Below you can see the handleEvent that does all this for you.

 1. /**
 2.  * Handle events that occur in the presentation.
 3.  */
 4. public boolean handleEvent(Event evt) {
 5.     if ((evt.id == Event.ACTION_EVENT) && (evt.target instanceof Widget)) {
 6.         Widget w = (Widget)evt.target;
 7.         String nm = w.getName();
 8.
 9.         // The user has clicked Ok, or the username was entered.
10.         if (nm.equals("okButton")) {
11.             String name = util.getText("userName").trim();
12.             if (name.length() > 0) {
13.                 System.out.println("Name=" + name);
14.                 System.exit(1);
15.             }
16.             util.setText("message", "You have to enter your name first!");
17.             return true;
18.         }
19.
20.         // The user has clicked Cancel, exit the application
21.         if (nm.equals("cancelButton")) {
22.             System.out.println("Cancelled");
23.             System.exit(0);
24.             return true;
25.         }
26.     }
27.     return super.handleEvent(evt);
28. }


What happens is the following: first of all in line 5 we check if the event is an action event from a widget. If that is true than a button must have been pressed. We can get the widget from the event because the widget is sent with the action event as the target, this happens in line 6.. Then we can see if the pressed button was the OK button by checking the name in line 10. In line 21 the same is done for the Cancel button.

If the OK button was pressed we check whether the user typed anything. If that is true then the name is printed to the console in line 13 and the application exits. If that is not true we display the message "You have to enter your name first" at the top. Because we gave that textbox the name "message" we can give that name to setText along with the new text in line 16. As you can see here, al the functions for accessing widgets are defined in util, a PlayerUtil. In line 17 handleEvent returns true, which means that the event has been handled.
If the Cancel button was pressed the application prints "Cancelled" to the console in line 22. Then it exits the application.
And if the event was not handled at all, we let the implementation of handleEvent in the superclass handle this event. The superclass can then decide to handle it or pass it along to it superclass.

Now what we have is a class Username that extends PlayerFrame, so it can play a presentation. We are handling action events for the OK and Cancel button. All we have to do now is write a main method that sets the right presentation and shows it.
The main method:

/**
 * The main program.
 */
public static void main(String argv[]) {
    // Create the frame
    UserName frm = new UserName();
    frm.util.setPresentation("username.gui");
    frm.show();
}

It creates a new UserName object and sets the presentation of that UserName to "username.gui". After that it shows the presentation and is done.

You can also see the entire code of the class UserName in the demo/programming directory in the Marimba Bongo directory.


Back to the tutorial index