Programming Language Concepts: Lecture 8

Similar documents
CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

MIT AITI Swing Event Model Lecture 17

Programming Language Concepts: Lecture 7

Window Interfaces Using Swing Objects

Systems Programming Graphical User Interfaces

Programming Languages and Techniques (CIS120e)

Window Interfaces Using Swing Objects

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Graphical User Interfaces (GUIs)

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Programming Language Concepts: Lecture 9

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Swing from A to Z Some Simple Components. Preface

Lecture 5: Java Graphics

Lecture 3: Java Graphics & Events

CSE 8B Intro to CS: Java

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

G51PRG: Introduction to Programming Second semester Applets and graphics

Class 16: The Swing Event Model

CS Programming Language - JAVA. Fall 2004 Oct.13rd

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

GUI Programming: Swing and Event Handling

Graphical Interfaces

Java - Applets. public class Buttons extends Applet implements ActionListener

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

User interfaces and Swing

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Packages: Putting Classes Together

H212 Introduction to Software Systems Honors

Graphical User Interface (GUI)

Introduction to the JAVA UI classes Advanced HCI IAT351

RAIK 183H Examination 2 Solution. November 10, 2014

Graphical Interfaces

(Incomplete) History of GUIs

RAIK 183H Examination 2 Solution. November 11, 2013

Java. GUI building with the AWT

Part 3: Graphical User Interface (GUI) & Java Applets

SINGLE EVENT HANDLING

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Original GUIs. IntroGUI 1

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Computer Science 210: Data Structures. Intro to Java Graphics

AP CS Unit 11: Graphics and Events

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

GUI Forms and Events, Part II

JAVA NOTES GRAPHICAL USER INTERFACES

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Introduction. Introduction

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Programming Languages and Techniques (CIS120)

Graphical User Interface (GUI)

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CHAPTER 2. Java Overview

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CSE 219 COMPUTER SCIENCE III BEHAVIORAL DESIGN PATTERNS SLIDES COURTESY: RICHARD MCKENNA, STONY BROOK UNIVERSITY

Swing from A to Z Using Focus in Swing, Part 2. Preface

7. Program Frameworks

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Graphic User Interfaces. - GUI concepts - Swing - AWT

1.00 Lecture 14. Lecture Preview

Unit 7: Event driven programming

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Class 14: Introduction to the Swing Toolkit

Graphical Applications

Chapter 1 GUI Applications

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Programming Language Concepts: Lecture 6

Part I: Learn Common Graphics Components

Java: Graphical User Interfaces (GUI)

CS 251 Intermediate Programming GUIs: Components and Layout

1005ICT Object Oriented Programming Lecture Notes

Graphical interfaces & event-driven programming

MVC: Model View Controller

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

CS415 Human Computer Interaction

IT101. Graphical User Interface

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Java Help Files. by Peter Lavin. May 22, 2004

Swing: Building GUIs in Java

BM214E Object Oriented Programming Lecture 13

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Programming Languages 2nd edition Tucker and Noonan"

Transcription:

Programming Language Concepts: Lecture 8 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 8, 11 February 2009

GUIs and event driven programming How do we design graphical user interfaces?

GUIs and event driven programming How do we design graphical user interfaces? Multiple applications simultaneously displayed on screen

GUIs and event driven programming How do we design graphical user interfaces? Multiple applications simultaneously displayed on screen Keystrokes, mouse clicks have to be sent to appropriate window

GUIs and event driven programming How do we design graphical user interfaces? Multiple applications simultaneously displayed on screen Keystrokes, mouse clicks have to be sent to appropriate window In parallel to main activity, record and respond to these events Web browser renders current page Clicking on a link loads a different page

Keeping track of events Low level solution Remember coordinates and extent of each window

Keeping track of events Low level solution Remember coordinates and extent of each window Track coordinates of mouse

Keeping track of events Low level solution Remember coordinates and extent of each window Track coordinates of mouse OS reports mouse click at (x,y) Check which windows are positioned at (x, y) Check if one of them is active Inform that window about mouse click

Keeping track of events Low level solution Remember coordinates and extent of each window Track coordinates of mouse OS reports mouse click at (x,y) Check which windows are positioned at (x, y) Check if one of them is active Inform that window about mouse click Tedious and error-prone

Keeping track of events... Better solution Programming language support for higher level events Button was clicked, box was ticked...

Keeping track of events... Better solution Programming language support for higher level events Button was clicked, box was ticked... OS reports low level events Mouse clicked at (x, y), key a pressed

Keeping track of events... Better solution Programming language support for higher level events Button was clicked, box was ticked... OS reports low level events Mouse clicked at (x, y), key a pressed Run time support for language maps low level events to high level events

Keeping track of events... Better solution... Programmer directly defines components such as windows, buttons,...that generate high level events

Keeping track of events... Better solution... Programmer directly defines components such as windows, buttons,...that generate high level events Each event is associated with a listener that knows what to do e.g., clicking Close window exits application

Keeping track of events... Better solution... Programmer directly defines components such as windows, buttons,...that generate high level events Each event is associated with a listener that knows what to do e.g., clicking Close window exits application Programming language has mechanisms for Describing what types of events a component can generate Setting up an association between components and listeners

Keeping track of events... Better solution... Programmer directly defines components such as windows, buttons,...that generate high level events Each event is associated with a listener that knows what to do e.g., clicking Close window exits application Programming language has mechanisms for Describing what types of events a component can generate Setting up an association between components and listeners Different events invoke different functions Window frame has Maximize, Iconify, Close buttons

Keeping track of events... Better solution... Programmer directly defines components such as windows, buttons,...that generate high level events Each event is associated with a listener that knows what to do e.g., clicking Close window exits application Programming language has mechanisms for Describing what types of events a component can generate Setting up an association between components and listeners Different events invoke different functions Window frame has Maximize, Iconify, Close buttons Language sorts out events and automatically calls the correct function in the listener

An example A Button with one event, the button being pressed

An example A Button with one event, the button being pressed Pressing the button invokes the function buttonpush(..) in a listener interface ButtonListener{ public abstract void buttonpush(...); } class MyClass implements ButtonListener{... public void buttonpush(...){... // what to do when a button is pushed } } Button b = new Button(); MyClass m = new MyClass(); b.add_listener(m); // Tell b to notify m when pushed

An example... We have set up an association between Button b and a listener ButtonListener m

An example... We have set up an association between Button b and a listener ButtonListener m Nothing more needs to be done!

An example... We have set up an association between Button b and a listener ButtonListener m Nothing more needs to be done! Communicating each button push to the listener is done automatically by the run-time system Information about the button push event is passed as an object to the listener buttonpush(...) has arguments Listener can decipher source of event, for instance

Timer Recall Timer example Myclass m creates a Timer t that runs in parallel Timer t notifies a TimerOwner when it is done via a function notify() In our example, Myclass m was itself the TimerOwner to be notified In principle, Timer t could be passed a reference to any object that implements TimerOwner interface

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT Relationship between components generating events and listeners is flexible

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT Relationship between components generating events and listeners is flexible One listener can listen to multiple objects Three buttons on window frame all report to common listener

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT Relationship between components generating events and listeners is flexible One listener can listen to multiple objects Three buttons on window frame all report to common listener One component can inform multiple listener Exit browser reported to all windows currently open

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT Relationship between components generating events and listeners is flexible One listener can listen to multiple objects Three buttons on window frame all report to common listener One component can inform multiple listener Exit browser reported to all windows currently open Must explicitly set up association between component and listener

Event driven programming in Java Swing toolkit to define high-level components Built on top of lower level event handling system called AWT Relationship between components generating events and listeners is flexible One listener can listen to multiple objects Three buttons on window frame all report to common listener One component can inform multiple listener Exit browser reported to all windows currently open Must explicitly set up association between component and listener Events are lost if nobody is listening!

A detailed example in Swing A button that paints its background red JButton is Swing class for buttons Corresponding listener class is ActionListener Only one type of event, button push invokes actionperformed(...) in listener Button push is an ActionEvent

A detailed example in Swing... class MyButtons{ private JButton b; public MyButtons(ActionListener a){ b = new JButton("MyButton"); // Set the label on the bu b.addactionlistener(a); // Associate an listener } }

A detailed example in Swing... class MyButtons{ private JButton b; public MyButtons(ActionListener a){ b = new JButton("MyButton"); // Set the label on the bu b.addactionlistener(a); // Associate an listener } } class MyListener implements ActionListener{ public void actionperformed(actionevent evt){...} // What to do when a button is pressed } class XYZ{ MyListener l = new MyListener(); // ActionListener l MyButtons m = new MyButtons(l); // Button m, reports to l }

A detailed example in Swing... To actually display the button, we have to do more Embed the button in a panel JPanel Embed the panel in a frame JFrame Display the frame!

A JPanel for our button... import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonPanel extends JPanel implements ActionListener{ private JButton redbutton; public ButtonPanel(){ redbutton = new JButton("Red"); // Create the button redbutton.addactionlistener(this); // Make panel a listener add(redbutton); // Embed button in panel } public void actionperformed(actionevent evt){ Color color = Color.red; // Set background colour setbackground(color); // to red when button repaint(); // is clicked } }

A JFrame for our panel... JFrame itself generates seven different types of events Corresponding listener class is WindowListener Each of the seven events automatically calls a different function in WindowListener

A JFrame for our panel... JFrame itself generates seven different types of events Corresponding listener class is WindowListener Each of the seven events automatically calls a different function in WindowListener Need to implement windowclosing event to terminate the window Other six types of events can be ignored

A JFrame for our panel... JFrame itself generates seven different types of events Corresponding listener class is WindowListener Each of the seven events automatically calls a different function in WindowListener Need to implement windowclosing event to terminate the window Other six types of events can be ignored One more complication JFrame is complex, many layers Items to be displayed have to be added to ContentPane

A JFrame for our panel... public class ButtonFrame extends JFrame implements WindowListener { Private Container contentpane; public ButtonFrame(){ settitle("buttontest"); setsize(300, 200); addwindowlistener(this); /// ButtonFrame listens to itself contentpane = this.getcontentpane(); // ButtonPanel is added contentpane.add(new ButtonPanel()); // to the contentpane } // Seven methods required for implementing WindowListener // Six out of seven are dummies (stubs) public void windowclosing(windowevent e){ // Exit when window System.exit(0); // is killed } } public void windowactivated(windowevent e){}... // 5 more dummy methods

Finally, a main function import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(string[] args) { JFrame frame = new ButtonFrame(); frame.show(); } }

Three buttons A panel with three buttons, to paint the panel red, yellow or blue Make the panel listen to all three buttons Determine what colour to use by identifying source of the event

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonPanel extends JPanel implements ActionListener{ private JButton yellowbutton; private JButton bluebutton; private JButton redbutton; // Panel has three buttons public ButtonPanel(){ yellowbutton = new JButton("Yellow"); bluebutton = new JButton("Blue"); redbutton = new JButton("Red"); yellowbutton.addactionlistener(this); // ButtonPanel is the bluebutton.addactionlistener(this); // listener for all redbutton.addactionlistener(this); // three buttons } add(yellowbutton); add(bluebutton); add(redbutton);

public class ButtonPanel extends JPanel implements ActionListener{... public void actionperformed(actionevent evt){ Object source = evt.getsource(); // Find the source of the // event Color color = getbackground(); // Get current background // colour if (source == yellowbutton) color = Color.yellow; else if (source == bluebutton) color = Color.blue; else if (source == redbutton) color = Color.red; } } setbackground(color); repaint();