next up previous
Next: Lecture 10 Up: Home

Readings



Graphical User Interface - GUI



GUI Elements



GUI Elements defined in java.awt

Class Hierarchy

GUI Event-Driven programs



Thread one, sets up communication & links &
instantiates applet 1-3,4-6 call init A-C

Mimic

Mimic_GUI

Mimic_Action_Listener

java.applet.Applet        Label
   ^                      ,String------.
   | extends     A------->`--------.---'
 Mimic          /        Mimic_Gui ^        TextField
,-------------./        ,----------2--.  ,-----------------.
| gui --------|----1--->|          |  |  |                 |
|             |         |   label--'  |  |BlackBox contains|
|             |----B----|-> quote --3-|->|String, listener |
|             |         |             |  | receives        |
|             |         |             |  | ActionEvents    |
|             |<---4----|-- applet    |  | events          |
|             |      ,--|-- listener  |  |                 |
|-------------|      |  |-------------|  |-----------------|
| init()      |      |  | init()      |  |method that calls|
| update_label|      5  | update_label|  |                 |
|             |      |  | (String)    |  |addActionListener|
|             |      |  | get_quote() |  |(listener)       |
`----.--------'      |  `-------------'  `---.-------------'
     ^               V                       |
     |        ,------------------.           |
     `---6----|-listening_applet |<----C-----'
              |------------------|
              | actionPerformed  |
              `------------------'
             Mimic_Action_Listener
                  | implements
                  V interface
    java.awt.event.Action_Listener


Applet: Thread 1 sets up communications

A&B) applet.add(Component) places
components in the applet container

C) Registering quote.addActionListener (listener)
associates the listening object with the quote TextField (a component) and a new thread is started. i.e. connect an ActionEvent with a listener

TextField: Thread 2 waits for and
responds to an ActionEvent

When the quote object detects the ActionEvent, a newline in the text field, all listeners registered using
addActionListener are called. i.e.
specific methods are called.

Listener classes must provide specific methods i.e. in this case actionPerformed(ActionEvent) from ActionListener is implemented.


Event-Driven Programming

java.applet.Applet        Label
   ^                      ,-String-----.
   | extends     .------->`--------.---'
 Mimic          /        Mimic_Gui ^        TextField
,-------------./        ,-------------.  ,-----------------.
| gui --------|-------->|          |  |  |                 |
|             |         |   label--'  |  |BlackBox contains|
|             |---------|-> quote ----|->|String, listener |
|             |         |             |  | receives        |
|             |         |             |  | ActionEvents    |
|             |<--------|-- applet    |  | events          |
|             |      ,--|-- listener  |  |                 |
|-------------|      |  |-------------|  |-----------------|
| init()      |      |  | init()      |  |method that calls|
| update_label|      |  | update_label|  |                 |
|             |      |  | (String)    |  |addActionListener|
|             |      |  | get_quote() |  |(listener)       |
`----.--------'      |  `-------------'  `---.-------------'
     ^               V                       |
     |        ,------------------.           |
     `--------|-listening_applet |<----------'
              |------------------|
              | actionPerformed  |
              `------------------'
             Mimic_Action_Listener
                  | implements
                  V interface
    java.awt.event.ActionListener


TextField: Thread 2

When a newline character is entered in TextField quote.actionPerformed(ActionEvent) is called beginning the sequence that updates the label component

Class                 Object calls Method
-----                 ------ ------------
TextField            [quote] actionPerformed(ActionEvent)
Mimic_Action_Listener applet update_label()
Mimic                 gui    get_quote()
Mimic_GUI             quote  getText()
Mimic                 gui    update_label(String)
Mimic_GUI             label  setText(String)


Event Interfaces Appendix O page 663



EventObject <-- AWTEvent <-- Event classes


Event Class Listener Interface
Methods implemented
AssociationGenerated by
Component Class
ActionEvent interface ActionListener
actionPerformed(ActionEvent)
addActionListener Button
List
TextField
AdjustmentEvent interface AdjustmentListener
adjustmentValueChanged(AdjustedEvent)
addAdjustmentListener Scrollbar


Thread two in Event-Driven Programming

              output  ,-----.
                ,---->| GUI |
no             /      `-----'
input,--------/    _____.<-----------.
 o---| applet |<--|queue|<----.      |
     `--------'   `-----'     |      |
      |  |  ,---------.   ,--------. |
      |  `->|component|-->|listener| |
input |     `---------'   `--------' |
      |     ,---------.   ,--------. |
      `---->|component|-->|listener|-'
            `---------'   `--------'

input Actions include keystrokes,
mouse movements, mouse keystrokes


Components and Containers

Components provide



Containers



Components Hierachies



GUI Components

Labels



TextFields and TextAreas extend
TextComponent



Lists Selectable list labeled with Strings

Buttons



Scrollbars - See Zoom class

Zoom Source



Layout Managers

setLayout(new FlowLayout());

The Flow Applet:

WIDTH=300 HEIGHT=150WIDTH=150 HEIGHT=150

Flow Source


The Grid Applet:

300 x 150200 x 150

Grid Source


The Border Applet:

doesn't work

Border Source


The Card Applet:

doesn't work

Card Source


The Grid_Bag Applet:

Grid_Bag Source




next up previous
Next: Lecture 10 Up: Home

Craig MacDonald
Wed Jul 15 15:12:37 EDT 1998