Computer Science 161

Laboratory Exercise #6

Thursday, December 3, 2008

A brief introduction to graphical user interfaces (GUI) in Java:  Building Command Interfaces in Java.

In this laboratory exercise we look at three ways in which we can communicate with a user using the Java graphical user interface (GUI).  We begin by first looking at the JOptionPane methods we have used before.  We then look at another JOptionPane example which allows us to build a more sophisticated button-driven menu.  We finally begin to look at some of the layout features of Java windows and see an introduction to capturing mouse actions using the ActionListener interface (more on interfaces Friday).

Notes on the current assignment:

Step 1:  Using what we already know about Dialog boxes to build a command interface

In class Tuesday (12/2/08) we discussed a procedure getValidInput which worked with the user using System.out.println and the Reader class to present the user with a menu of choices and kept on asking until the user entered an invalid command.

Write a simple class by creating a new project and clicking on the New Class button.  Accept the default class (we will not use the class in this exercise;  Write a simple private static int getValidInput() routine which throws up a JOptionPane.showInputDialog window with the messsage listing the four possibilities for command:

       String message1 = "Wecome to the Inventory Management System\n" +

            "Please enter a command:  0 to add a part\n" +

            "                         1 to delete a part\n" +

            "                         2 to list all parts\n" +

            "                         3 to quit";

Before converting the resulting String from showInputDialog we must consider what happens when the user clicks "CANCEL" or the close box.  In both of these cases showInputDialog returns a null string.  If we pass a null string to Integer.valueOf, we get an exception (more on exceptions next week).  So we need to check first:

       if (commandString == null) command = QUIT;

        else command = Integer.valueOf(commandString); to quit";

Where QUIT is a con.  stant defined earlier.

Try this out.  To see a completed example, copy GUI01 from the Handouts folder on Hedwig.

Step 2:  Using a more sophisticated version of JOptionPane.showInputDialog

In this example, we revisit the showOptionDialog method used earlier in this laboratory exercise and discussed in more detail in an earlier laboratory exercise (Laboratory exercise #3)..  This approach will introduce a few new fields in our discussion on showOptionDialog.

Several have already discovered that there are several methods described in the JOptionPane documention titled showInputDialog.  They differ from each other in the signature of the method.  For this example, we want to use the method with the signature

public static int showOptionDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon,
                                     Object[] selectionValues,
                                     Object initialSelectionValue)
                              throws HeadlessException

(this and the following is copied verbatim from the Java Documentation)

The parameters are as follows:

Try this out (you can use a modification of the methods (including main) you wrote above.  The Java class GUI02 in the handouts folder gives a completed example of all this.

Step 3:  Interacting with the user through mouse clicks, etc.:  The ActionListener interface

Finally, we take a brief look at the ActionListener.