CISC 370 Lecture Notes for Class No. 12 March 16, 2000

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Assignment

Reading:
Horstmann & Cornell, remainder of chap. 8
Exercises: Graded HW #8, Due Tues, April 4

1. Add buttons "First" and "Last" to the GUI for the picture gallery that will cause the display of the first and last image respectively. Add appropriate methods to the ImageGallery class as needed. To start on this exercise, first copy of the .java files from the directory $CISC370HOME/exampl-progs/buttons/pictureGalleryWButtons to a subdirectory of your home directory.

To prepare programming assignments for submission, do the following for each exercise.

script	        // Produces a file named typescript
whoami 
date 
pwd 
cat *.java 
javac *.java     // mainFile should be the name of the file containing
java mainFile    // your main() method
exit		 // to exit script

Now print the typescript file for submittal at the beginning of lecture.

On strauss, you can use xv as demoed in lecture to grab and print a copy of the graphics output from your Java programs that should be handed-in with your assignment.

Today's Lecture Topics

  1. An review of containers, components, layout managers, and event handlers (listeners)

  2. A first example of an event handler for a button component in a frame container. In this example, the button is a simple toggle switch that toggles the background of the frame between black and white. This code can be found in the directory example-progs/buttons/firstButton.

    The new features/ideas to particularly note in this example are the following:

    1. In the ButtonPanel constructor note the creation of the onOffButton as an object of the JButton class and its placement on the ButtonPanel via the add() method of the Container class, which is inherited by the JPanel class.
    2. The use of the addActionListener() method in the ButtonPanel constructor to register the onOffButton event handler with the button.
    3. The actionPerformed() method of the ButtonPanel class that must be there to implement the ActionListener interface. This method toggles the background color between black and white and calls the repaint() method to schedule repainting of the panel.
    4. The SimpleFrame class has a main() method for testing this code. It contains nothing new that has not been used in other frame classes in previous examples.

  3. A more complex example of using button controls . In this example, we add two buttons, previous and next, to our picture gallery example that allow the user to step forward and backwards through the images. The code for this example is in the directory example-progs/buttons/pictureGalleryWButtons.

    The new features/ideas to particularly note in this example are:

    1. The addition of the methods decrImageNo() and incrImageNo() to the ImageGallery class. These should have been in the version of ImageGallery found in the directory example-progs/pictureGallery/version2.
    2. In the ButtonPanel class note the two instance variables prevButton and nextButton, their initialization in the constructor with JButton objects, their addition to the ButtonPanel, and the registration of the event handler for each. Also note the implementation of the actionPerformed() method that serves as the event handler for both buttons. It calls the decrImageNo() or incrImageNo() method of the ImageGallery class as is appropriate for the button pushed. Those methods then notify the observer which causes the new image to be displayed.
    3. The IGDisplayer class is the same as the one found in the directory example-progs/pictureGallery/version2.
    4. The ImageFrame class contains a main method for testing the code in this example. It initializes the URLs and titles for the images as has been done in the previous picture gallery examples. It then creates the image gallery, IG displayer, and button panel objects. A frame is created and the IG displayer and button panels are added to the frame and the frame is shown.

Back to the CISC 370 homepage.

This page has been accessed times since 20 Mar 2000.

Corrections, suggestions and comments to Bob Caviness

Copyright 2000 B. F. Caviness