CSci 121 Lab 4: Graphical User Interfaces

Objectives

The objective of this lab is simply to extend the BagJApplet class so that it tests more features of the class IntArrayBag. Along the way, you will become more familiar with using Java GUI components and event-driven programming.

Exercises

  1. Start by downloading IntArrayBag.java, BagJApplet.java. Put them both into a BlueJ project.
  2. Compile both files and run the applet. This is done by right-clicking on the BagJApplet class and choosing "Run". This will give you a "Run Applet" box in which you will select the "Run applet in appletviewer" button, and enter "800" for the width (the default of 500 for the height should be fine). When the applet starts up, you can enter numbers into the text fields and push the buttons.
    Play with it for a few minutes.
  3. Add a toString() method to the IntArrayBag class. For example, if the bag contains the elements {3, 4, 1, 3, 4, 5}, then toString() might return the string,
    "3 4 1 3 4 5"
    (although, this being a bag, if they end up in reverse order it doesn't matter). The idea for the implementation here is to simply traverse the array and concatenate each element into a string, finally returning the string. Test the toString() method using BlueJ: Create an IntArrayBag object, add some elements to it by invoking the add() method, and invoke the object's toString() to see that something nice comes out.
    Now we will see how to test toString() by extending the BagJApplet GUI.
  4. Let's add a button that will result in the contents of the entire bag being displayed in the TextArea. To start with, put a declaration for the button (printButton might be a good name for it) near the beginning of the init() method. Also add the code to create the button, giving it a label (like "print"). To see how this is done, take a look at how the size button is coded. This one will be similar; if you have any doubts as to what to do in this or the next 4 steps, take a look at the relevant code for the size button.
  5. In the BagJApplet class, add an inner class that will be used to create the ActionListener for the print button. It should, of course, have an actionPerformed() method in it. It might be a good idea to leave the method empty and make sure everything compiles before you proceed.
  6. Go back to init(), if you haven't already done so, and add code that registers the appropriate ActionListener with the print button.
  7. Now think about the code for actionPerformed(). In fact, this is exactly where you will test the toString() method. "Printing" to the TextArea is easily done via activations of the form feedback.append(stuff to print). The syntax for stuff to print is exactly like the argument in a System.out.print(stuff to print) statement. (The one problem is there's no analogue of System.out.println(), but that's not a big problem. To stay on the same line, don't print the character "\n". To go to the next line, DO print the character "\n".)
  8. Test your print button. Now, each time you add something to the bag, you can also push the print button to see if what you've added is really there.
  9. Following the pattern in the code for the add() button, and essentially following the procedure outlined above, add a button to test the remove() method. This is a little more involved than the print button, since you also must add a text field to enter the element to remove. As for actionPerformed(), that should be easy, since you can follow the code given in the corresponding method for the add() button, except now you remove rather than add.
  10. Probably many of you know that there are other ways to run applets: Using the appletviewer command, or using a web-browser. Give each of these a try.
  11. To use the appletviewer directly (you were using it indirectly when you ran it from BlueJ), go to the terminal window, and using the "cd" command, go to the folder in which BlueJ placed this project. Do "ls" to see that there is a file named BagJApplet.html in the directory. BlueJ automatically created this file when you ran the applet, but of course you can manually create html files. To run with the appletviewer, just type:
    appletviewer BagJApplet.html &.
    and the applet should run.
  12. To run it from a browser, in the browser choose "File/Open File" (or equivalent, depending on your browser), navigate to the project folder, and open BagJApplet.html. Assuming everything is configured properly, the applet will run in the browser. It ought to look something like this (although that link is just to an applet that uses the "starter code").
  13. Hand in a printout of your final IntArrayBag.java and BagJApplet.java code.

Last modified: 2/12/07 by Frederic Green.