Reacting to the user
Outline
Sequential programming GUI program organization Event-driven programming Modes
Sequential Programming
In sequential programs, the program is under control The user must synchronize with the program:
Program tells user it is ready for input User enters input and it is processed
Examples:
Command-line prompts (DOS, UNIX) LISP interpreters
Shouldn’t the program be required to synchronize with the user?
Sequential Programming (2)
Flow of a typical sequential program
Prompt the user Read input from the keyboard Parse the input (determine user action) Evaluate the result Generate output Repeat
Example
DemoTranslateEnglishConsole.java
Prompt the user User input Output results
Sequential Programming (3)
Advantages
Architecture is iterative (one step at a time) Easy to model (flowcharts, state machines) Easy to build
Limitations
Can’t implement complex interactions Only a small number of features possible Interaction must proceed according to a pre-defined sequence
To the rescue… Event-driven programming But first…
Outline
Sequential programming GUI program organization Event-driven programming Modes
GUI Program Organization
Let’s digress briefly to examine the organization of our GUI programs We’ll do this in stages, by examining three example programs
DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.java
Outline
Sequential programming GUI program organization Event-driven programming Modes
Event-driven Programming
Instead of the user synchronizing with the program, the program synchronizes with, or reacts to, the user All communication from user to computer occurs via events and the code that handles the events An event is an action that happens in the system A mouse button pressed or released A keyboard key is hit A window is moved, resized, closed, etc.
Classes of Events
Typically, two different classes of events
User-initiated