









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to object-oriented graphical user interface (gui) programming using the model-view-controller (mvc) design pattern. It covers the history of gui development, the components of mvc, and event handling in java. The document also includes steps for creating a gui in java using swing and the event dispatching thread (edt).
Typology: Study notes
1 / 16
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)
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)
Programming Models
Normal (control flow-based) Programming
Approach Start at main() Continue until end of program or exit()
Event-driven Programming
Event - Action or condition occurring outside normal flow of control of program (e.g., mouse clicks, keyboard input, etc.) Unable to predict time & occurrence of event Approach Start with main() Define system elements and register event listeners Await events (& perform associated computation)
7
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)
GUIs in Java
Swing
Java Runtime Environment
AWT Java 2D
Desktop Java Graphics APIs: From “Filthy Rich Clients” by Chet Haase and Romain Guy, Chap1, Page 12 ISBN-978-0-13-241393- Book Web Site: http://www.filthyrichclients.org/
Steps for Creating a GUI in Java
Examples: JFrame, JApplet…
Examples: JButton, JTextField, JScrollBar… Use layout manager to determine positions
Add event listeners to GUI components
Step 1 (Define Container)
Abstractions occupying space in GUI
Usually contain one or more widgets widget - actual item user can see Can be nested in other containers
JFrame, JDialog, JPanel, JScrollPane
Step 3 (Set Event Listeners)
Implementation Implement event listeners for each event Usually one event listener class per widget Inner class usually utilized to implement listener Register (add) listener object with widget object
At run time Java generates event object when events occur Java then passes event object to event listener
Example of Java listeners & Actions Causing Event ActionListener clicking button in GUI CaretListener selecting portion of text in GUI FocusListener component gains / loses focus KeyListener pressing key MouseListener mouse clicked WindowListener closing a window
Step 4 (Schedule GUI Processing in EDT)
What is a thread? Event Dispatching Thread (EDT) EDT is a background thread to process events These events are mainly updates that Cause components to redraw themselves Represent input events Swing uses a single-threaded painting model Event Dispatching thread is the only valid thread for updating GUI components Avoid updating GUI components from other threads A source of common bugs
Additional Resources
http://java.sun.com/docs/books/tutorial/uiswing/components/
http://filthyrichclients.org/