










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Graphical User Interface (GUI)
Interface between user and computer Both input and output Affects usability of computer
Switches & light bulbs Punch cards & teletype (typewriter) Keyboard & black/white monitor (text) Mouse & color monitor (graphics)
Model-View-Controller (MVC)
Model
View
Controller
MVC Model of GUI Design
Should perform actual work Should be independent of the GUI But can provide access methods
Lets user control what work the program is doing Design of controller depends on model
Lets user see what the program is doing Should not display what controller thinks is happening (base display on model, not controller)
Creating a GUI in Java
Examples: JFrame, JPanel, JApplet…
Examples: JButton, JTextField, JScrollBar… Use layout manager to determine positions
Add event listeners to GUI components
GUI Elements 1 – Container
Abstractions occupying space in GUI
Usually contain one or more widgets Can be nested in other containers
JFrame, JDialog, JPanel, JScrollPane
GUI Elements 3 – Events
Action or condition occurring outside normal flow of control of program
Mouse clicks Keyboard input Menu selections Window actions
Programming Models
Approach Start at main() Continue until end of program or exit()
Unable to predict time & occurrence of event Approach Start with main() Build GUI Await events (& perform associated computation)
Dispatcher
Event Handling in Action
Events
E 4
E 5
E 6
E 7
E 8
E 1
E 2
E 3
Registered Event Handlers
e 1
e 2
e 3
State =State =State =State = SSSS 0123
e 3
E 1 E^2 E^3
E 4 E^5 E^6
E 7
E 8
Event Handlers (^) Can handle an event of type e 1
Execution Environment
15
Event Handlers
User events invoke event handlers
GUIs are Event-Driven Software
E (^3) E 2 E 1 E 4
E 5
User Events
changeFontSizeActionPerformed (java.awt.event.ActionEvent evt)
newDocActionPerformed (java.awt.event.ActionEvent evt)
fileSaveActionPerformed (java.awt.event.ActionEvent evt)
Event Dispatching Thread
From AWT graphical interface event queue
Cause components to redraw themselves Represent input events
Event Dispatching thread is the only valid thread for updating GUI components Avoid updating GUI components from other threads A source of common bugs
Event Dispatching Thread
Allows current thread to execute GUI code in dispatching thread createAndDisplayGUI Method that actually defines the GUI
javax.swing.SwingUtilities.invokeLater(new Runnable( ) { public void run( ) { createAndDisplayGUI( ); } });