Docsity

Object-Oriented Programming II: GUI - Univ. of Maryland, College Park - Prof. Nelson Padua, Study notes of Computer Science

Partial preview of the text

Download Object-Oriented Programming II: GUI - Univ. of Maryland, College Park - Prof. Nelson Padua and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Graphic User Interface (GUI)

Department of Computer Science

University of Maryland, College Park

Graphical User Interface (GUI)

User interface

Interface between user and computer Both input and output Affects usability of computer

Interface improving with better hardware

Switches & light bulbs Punch cards & teletype (typewriter) Keyboard & black/white monitor (text) Mouse & color monitor (graphics)

Model-View-Controller (MVC)

Model for GUI programming (Xerox PARC ’78)

Separates GUI into 3 components

  1. Model  application data
  2. View  visual interface
  3. Controller  user interaction

Model

View

Controller

MVC Model of GUI Design

Model

Should perform actual work Should be independent of the GUI But can provide access methods

Controller

Lets user control what work the program is doing Design of controller depends on model

View

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

1. Define a container to hold components

Examples: JFrame, JPanel, JApplet…

2. Add GUI components to the container

Examples: JButton, JTextField, JScrollBar… Use layout manager to determine positions

3. Add actions to GUI

Add event listeners to GUI components

GUI Elements 1 – Container

Definition

Abstractions occupying space in GUI

Properties

Usually contain one or more widgets Can be nested in other containers

Examples

JFrame, JDialog, JPanel, JScrollPane

GUI Elements 3 – Events

Definition

Action or condition occurring outside normal flow of control of program

Examples

Mouse clicks Keyboard input Menu selections Window actions

Programming Models

Normal (control flow-based) programming

Approach Start at main() Continue until end of program or exit()

Event-driven programming

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

GUI

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

Background thread to process events

From AWT graphical interface event queue

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

Event Dispatching Thread

Example code

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( ); } });