CISC 370 Lecture Notes for Class No. 13 March 21, 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, chap. 9
Exercise (for extra credit). Due 4/4/00:

1. The Color Mixer application, described below and in the accompanying code, generates six listener objects, one for each of the three scroll bars and one for each of the three text fields. Rewrite the application using only two listener objects, one for the three scroll bars and one for the three text fields. To do this, make use of the method getSource() and the instance variables for the scroll bars and text fields that are defined for the class ScrollTest.

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. More GUI components and event handlers. The ubiquitous color mixer and displayer. Compare this example to a slightly different version found in H&C, vol I, on pp. 441-5.

    Things to notice about this example.

    • The way the corresponding scroll bar and text fields control each other.
    • The class ScrollCommand provides for event handling for scroll bars by implementing the AdjustmentListener interface.
    • The class TextCommand provideds for event handling for text fields by implementing the ActionListener interface. You might think (quite reasonbly I might add - this is a design fault of awt.event package in my opinion) that you should implement the TextListener interface. TextListener listens to all events in a text field. ActionListener listens only to those events that are followed by a carriage return. The former provides control that is too fine for this application so the latter is preferred.
    • JLabel components are used to label the text field and scroll bar components. Labels are described on pp. 404-5 of Horstmann & Cornell.
    • The constructor for scroll bars takes the following args:
      public Scrollbar(int orientation,
                        int value,
                        int visible,
                        int minimum,
                        int maximum)
      
      Constructs a new scroll bar with the specified orientation, initial value, page size, and minimum and maximum values.

      The orientation argument must take one of the two values SwingConstants.HORIZONTAL, or SwingConstants.VERTICAL, indicating a horizontal or vertical scroll bar, respectively. SwingConstants is an interface that contains named constants used by various Swing classes. Interfaces can define constants in addition to specifying prototypes for methods.

      If the specified maximum value is less than the minimum value, it is changed to be the same as the minimum value. If the initial value is lower than the minimum value, it is changed to be the minimum value; if it is greater than the maximum value, it is changed to be the maximum value.

      Parameters:

               orientation - indicates the orientation of the scroll bar. 
               value - the initial value of the scroll bar. 
               visible - the size of the scroll bar's bubble, representing the visible
                         portion; the scroll bar uses this value when paging up or 
                         down by a page.   This is for use when a scroll bar is
                         used for scrolling on a page.  Scrollbar components are
                         not used much for such applications anymore since the
                         JScrollPane class was introduced in Java 2.0.
               minimum - the minimum value of the scroll bar. 
               maximum - the maximum value of the scroll bar. 
      
      In our application the third param, visible, has nothing to do with the visible portion of the page that is visible, but does effect how finely we can control the scrollbar. Smaller values give more finely grained control.

      For more information about scroll bars, see Horstmann & Cornell, Vol I, pp. 441-5.

      Text fields, used for text input in GUIs, are described on pp. 386-393.

  2. Here is a nice online tutorial from Sun on writing event listeners . In particular, you may want to bookmark the URL of listeners supported by Swing components so that you can quickly find this quick reference table.

  3. For a good demo of many of the Swing components, see SUN's SwingSet demo . This is a large Java applet and takes a long time to load so be patient. This is a good demo for getting ideas of components to use with term projects.

Back to the CISC 370 homepage.

This page has been accessed times since 25 Mar 2000.

Corrections, suggestions and comments to Bob Caviness

Copyright 2000 B. F. Caviness