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

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

Systems Programming Graphical User Interfaces

Programming Languages and Techniques (CIS120e)

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

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

GUI Event Handlers (Part I)

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Advanced Java Programming (17625) Event Handling. 20 Marks

The AWT Event Model 9

Window Interfaces Using Swing Objects

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

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

Class 16: The Swing Event Model

CS 251 Intermediate Programming GUIs: Event Listeners

Swing from A to Z Some Simple Components. Preface

Chapter 1 GUI Applications

Programming Language Concepts: Lecture 8

Window Interfaces Using Swing Objects

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

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

GUI Programming: Swing and Event Handling

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

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

COMPSCI 230. Software Design and Construction. Swing

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

CS 251 Intermediate Programming GUIs: Components and Layout

BM214E Object Oriented Programming Lecture 13

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Java IDE Programming-I

Graphic User Interfaces. - GUI concepts - Swing - AWT

Java: Graphical User Interfaces (GUI)

CSE 8B Intro to CS: Java

Programming Mobile Devices J2SE GUI

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!

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

CSE 331 Software Design & Implementation

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Graphical User Interface (GUI)

Jonathan Aldrich Charlie Garrod

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

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

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

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

Event Driven Programming

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Packages: Putting Classes Together

Graphical Interfaces

Lecture 19 GUI Events

CS11 Java. Fall Lecture 4

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

CS Exam 1 Review Suggestions

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Chapter 14. More Swing

PIC 20A GUI with swing

Dr. Hikmat A. M. AbdelJaber

Part I: Learn Common Graphics Components

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

MIT AITI Swing Event Model Lecture 17

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

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Graphical User Interfaces. Comp 152

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Building Graphical User Interfaces. GUI Principles

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

CS11 Java. Fall Lecture 3

Chapter 5: Enhancing Classes

WIMP Elements. GUI goo. What is WIMP?

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Graphical User Interface (GUI)

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

(Incomplete) History of GUIs

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical Interfaces

Lecture 5: Java Graphics

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

1005ICT Object Oriented Programming Lecture Notes

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

AP CS Unit 11: Graphics and Events

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

BASICS OF GRAPHICAL APPS

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Lecture 9. Lecture

GUI Forms and Events, Part II

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Transcription:

CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by the input data and by the control structures (e.g., "if" statements) used in the program. In the case of sequential control: Control starts are the first instruction in the main method Control flows from the present instruction to the next one until the last one is reached, at which point the program terminates. 2 Control Flow: Revisited An Example of Sequential Control In more abstract terms, the flow of control is: the thread(s) of execution in a software system, and their reaction to exogenous events, if any An exogenous event is an event from outside the system (e.g., user input, disk space becomes full, network connection is lost) Two primary mechanisms for control flow: Sequential Event Driven Example 2.1: DemoTranslateEnglishConsole.java Prompt the user User input Output results 3 4

Notes about Code Examples Notes about Ex 3.1 Examples in this class SHALL NOT make use of the packages type.io.*, type.util.* Use of these packages will not be allowed for the assignments Use the command line interface for compiling and invoking applications; use editor of your choice use your own Integrated Development Environment (IDE) if you wish (e.g., Eclipse) Mechanisms that allows user to exit program: provide sentinel CTRL-Z is null in Windows (CTRL-D in Unix) 5 6 Sequential Programs (1) Sequential Programs (2) Typical flow of control: Prompt the user Read input from the keyboard Parse the input (in order to interpret the user s action) Evaluate the result Generate output Continue until application determines it is time to stop (or until user terminates application) In sequential programs, control is held by the application: the application decides when the user may perform input actions application tells user whether it s ready for more input user enters more input and it is processed Examples: Command-line prompts (DOS, UNIX) LISP interpreters The user is required to respond to the program Shouldn t it be the other way around? Shouldn t the program respond to the user? 7 8

Sequential Programs (3) Event-driven Programs Advantages Architecture is iterative (one step at a time) Easy to model using flowcharts or finite state automata Relatively easy to build Limitations Difficult to implement complex interactions Only a small number of features may be practical to implement The sequence in which the interaction may proceed must be pre-defined All communication from the user to the application occurs via events An event is an action that happens: A mouse button pressed or released A keyboard key is pressed or released A window is moved, resized, closed, etc. Code is set up and waiting to handle these events 9 10 An Example of an Event-Driven Application Example 2.2: DemoTranslateEnglishGUI.java Notes about Ex 3.2 The main method has only four lines: public static void main(string[] args) DemoTranslateEnglishGUIFrame frame = new DemoTranslateEnglishGUIFrame(); frame.settitle("demotranslateenglishgui"); frame.pack(); frame.show(); 11 12

Identify the packages containing import... classes used in the program public class NameOfProgram Only one public class per file public static void main(string[] args) 1. Construct a JFrame 2. Give it a title 3. Cause the frame to be sized 4. Cause the frame to be visible public class NameOfProgramFrame import... public class NameOfProgram public static void main(string[] args) class NameOfProgramFrame extends JFrame The core of the application is found here Naming convention (just add Frame to name of program) 13 Our instance of JFrame is actually an sub-class instance it is a JFrame extended and modified to suit our needs 14 Basic Concepts Containment Hierarchy for JFC/Swing The graphical user interface consists of components all components are instances of JComponent (or child classes) Containers (aka non-atomic components) can contain other components (e.g., JTabbedPane) Atomic components cannot contain other components (e.g., JComboBox, JButton, JLabel) All components have a position within the containment hierarchy atomic components can exist only as leaves, but not all leaves need to be atomic a top-level container must be at the root of the hierarchy the three top level components are JFrame, JApplet, and JDialog 15 16 JButton JPanel JTextField JPanel JFrame Content pane JButton JLabel etc. JPanel etc.

What is a JFrame? Instantiating the JFrame 1. It is a window It has window decorations, such as borders, a titlebar and title, and buttons for closing and iconifying the window The style of these decorations is derived from the Look-and-Feel 2. It is a top-level container It has a content pane and a menu bar The menu bar is optional It is the root of a containment hierarchy 1. Must have a look and feel if not explicitly specified, resort to default 2. Must specify reaction to close operation otherwise, resort to default 3. Must add components to content pane otherwise, content pane will not appear, frame will consist only of titlebar must define components, add them to contentpane, then register listeners on them 17 18 Anatomy of a JFrame Icon Title Minimize Button Maximize/ Restore Down Button Close Button Title bar Different Types of Look and Feel for a JFrame Borders Content pane (grey region inside borders) Using Java s Look and Feel Using MS Window 2000 s Look and Feel An instance of a JFrame without a content pane 19 Using MS Window XP s Look and Feel 20

Instantiating the JFrame Examples 1. Must specify look and feel otherwise, resort to default [we ll use the dafault for the time being] 2. Must specify reaction to close operation otherwise, resort to default 3. Must add components to content pane otherwise, content pane will not appear, frame will consist only of titlebar must define components, add them to contentpane, then register listeners on them DemoVeryBasicV1, V2, V3, V4, V5 A trivial application to demonstrate some basic things about frames three different versions V1: behaviour for close action left as the default V2: behaviour for close action specified using method from JFrame V3,V4,V5: behaviour for close action specified using a method that is inherited from Window 21 22 Responding to the Close Operation Responding to the Close Operation An instance of a JFrame knows that something needs to be done when user performs a close on it close performed on the frame when the closeapplication button or keystroke is used rightmost button in top right-hand corner; Windows keyboard shortcut, ALT-F4 The default behaviour for close is to hide the window (see DemoVeryBasicV1) We ll next look at two ways to change this default 23 Option #1 (DemoVeryBasicV2) Invoke the following method in the constructor of the JFrame subclass: setdefaultcloseoperation(jframe.exit_on_close); (alternatively, can invoke the method on the instance of the JFrame in the main method) The default is: setdefaultcloseoperation(jframe.hide_on_close); See the API for the class JFrame for this method 24

Responding to the Close Operation DemoVeryBasicV4 Option #2 (DemoVeryBasicV3) Handle the operation with a registered WindowListener object. addwindowlistener( <instance of WindowListener> ); This method is inherited from the parent class of JFrame, which is Window therefore, need to import java.awt.event.* 25 class DemoVeryBasicFrame extends JFrame public DemoVeryBasicFrame () setdefaultcloseoperation(jframe.do_nothing_on_close); addwindowlistener( new WindowAdapter() public void windowclosing(windowevent e) System.out.println( "in windowclosing method" ); System.out.println( e ); * System.out.println( e.getsource() ); System.exit(0); ); * This is advanced; this code defines a WindowListener. That s all you need to know for now 26 How does this code work? What are Events? The method addwindowlistener registers the passed parameter on the instance of DemoVeryBasicFrame the passed parameter is an instance of a WindowListener What is a listener? A specialized piece of code that specifies what should happen for a specific type of event occurrence By specific type, we don t just mean the type of action, but rather the type of action with respect to a component We need to understand what events are and how they are created Each component in an application is a potential source of events When something happens, an instance of an event object gets created by built-in Swing code Events are represented by objects the instance itself contains information that identifies the source of the event An event always has a source for now, we will assume that the source is a component it fires the event 27 28

Java Events Types of Events Basic idea: set up listeners to detect when events of interest have occurred But the listening is always connected to a component An application registers listener objects on the various components An event listener must be installed for each components from which events may be generated otherwise, any events that are generated will pass undetected 29 There are two types of events: low-level and semantic A low-level event is: a window-system occurrence, or a low-level input (e.g., mouse button press, mouse button released, mouse button click (pressed and released), mouse cursor enter, mouse cursor exit, down, mouse up, key pressed, key released, key typed). A semantic event is any occurrence that is not a low-level event. 30 Types of Events Java s Event Class Hierarchy User Action Event that Occurs click a button ActionEvent press Enter while in a text field ActionEvent choose a menu item ActionEvent close a frame (main window) WindowEvent press a mouse button MouseEvent (while the cursor is over a component) move the mouse over a component MouseMotionEvent component becomes visible ComponentEvent component gets the keyboard focus FocusEvent Table or list selection changes ListSelectionEvent 31 ActionEvent EventObject AWTEvent MouseEvent InputEvent ComponentEvent KeyEvent A subset of Java s Event Class Hierarchy is shown here See Java API for full hierarchy WindowEvent 32

Example: DemoVeryBasicV3 Comments 1. The user presses the close button in the titlebar 2. This caused the creation of a WindowEvent 3. The source of the event is the instance of DemoBasicFrame 4. A WindowListener is registered on that instance; its windowclosing method gets invoked 5. The body of the windowclosing method invokes System.exit(0) 33 Any window state change generates an event: e.g., being opened, closed, activated or deactivated, iconified or deiconified Registered listeners are notified The relevant methods in the listener is invoked (depending on the type of WindowEvent). The instance of the WindowEvent is always passed to the method. Other types of events might be generated e.g., it the user mouses-down or presses keys in DemoBasicFrame MouseListener, KeyListener not registered, so no methods get invoked 34 Comments Instantiating the JFrame What happens if two listeners are registered? both listeners will hear the relevant events the methods will be invoked in the order in which the listeners were registered Each event source can have multiple listeners registered on it. Conversely, a single listener can register with multiple event sources. 1. Must specify look and feel otherwise, resort to default 2. Must specify reaction to close operation otherwise, resort to default 3. Must add components to content pane otherwise, content pane will not appear, frame will consist only of titlebar must define components, add them to contentpane, then register listeners on them 35 36

Example 2.4, four versions Adding Components DemoBasicV1, V2, V3, V4 A trivial application to demonstrate some basic things about adding components to frames four different versions V1: adding directly to contentpane V2: adding to contentpane via intermediate container, JPanel V3: adding ActionListener to a component, using an inner class V4: adding ActionListener to a component, without using an inner class 37 To start, let s use JLabel can display either text, an image, or both does not react to input events cannot get the keyboard focus JLabel samplelabel = new JLabel("Sample Label"); samplelabel.setfont(new Font("sanserif", Font.PLAIN, 16)); samplelabel.setpreferredsize(new Dimension(250, 100)); The methods setfont and setpreferredsize are inherited from JComponent 38 How do we add samplelabel to frame? Approach #1 access contentpane using method getcontentpane() add samplelabel to content pane directly How do we add samplelabel to frame? Approach #2 Construct a new JPanel, say panel add samplelabel to panel use method setcontentpane(panel) e.g., DemoBasic_v1 default LayoutManager for JFrame s contentpane is BorderLayout 39 e.g., DemoBasic_v2 default LayoutManager for JPanel is FlowLayout 40

Adding Other Components DemoBasicV3, DemoBasicV4 JButton can display either text, an image, or both an implementation of a "push" button generates events ActionEvent for mouse click also MouseEvent, KeyEvent togglebutton = new JButton("Press me!"); togglebutton.setfont(new Font("sanserif", Font.PLAIN, 16)); 41 JLabel JButton 42 Registering an ActionListener Creating an ActionListener Register by invoking the following from within constructor DemoBasicFrame constructor: togglebutton.addactionlistener( <*****> ); NOTICE!!!!! The parameter <*****> must be an instance of an ActionListener How do we define an ActionListener? 43 How do we define an ActionListener? ActionListener is an interface In fact, all of the various listeners (for all of the various types of events) are interfaces Recall that an interface cannot be instantiated What can we do? Interfaces can be implemented The compiler enforces the rule that if a class implements an interface, it must provide bodies for all of the methods defined in the interface 44

Example of a Class Implementing an Interface public abstract class WindowAdapter implements WindowListener void windowactivated(windowevent we) void windowclosed(windowevent we) void windowclosing(windowevent we) void windowdeactivated(windowevent we) void windowdeiconified(windowevent we) void windowiconified(windowevent we) void windowopened(windowevent we) More on this example So the class WindowAdapter implements the WindowListener interface Compiler will enforce rule that the class must provides a body for each of the methods defined in the interface But the compiler will allow a class to define all of the bodies to be empty!!! Q1: What do we call such a class? Q2: Why would we want such a class anyway? 45 46 More on Interfaces Listeners and Corresponding Adapters Q1: What do we call such a class? An adapter class The Java Foundation Classes (JFC) include several of these Q2: Why would we want such a class anyway? we can extend an adapter class and override selected methods it can be easier to do this with a adapter class from JFC than to implement the interface ourselves Listener interface (# methods) WindowListener (7) ActionListener (1) Later we ll discuss KeyListener (3) MouseListener (5) MouseInputListener (7)* ItemListener (1) FocusListener (2) Adapter class WindowAdapter not defined KeyAdapter MouseAdapter MouseInputAdapter not defined FocusAdapter 47 * MouseInputListener combines MouseListener and MouseMotionListener 48

Creating an ActionListener A: Using a Named Inner Classes (1) ActionListener is an interface; it cannot be instantiated Option A: create a named inner class that implements the interface e.g., DemoBasicV3 Option B: make the sub-class of JFrame implement the interface e.g., DemoBasicV4 Option C: create an anonymous inner class that implements the interface e.g., DemoVeryBasicV4 49 What is an inner class? A nested class is a class that is a member of another class. A non-static nested class is called an inner class. [The Java TM Tutorial, Implementing Nested Classes ] 50 A: Using a Named Inner Classes (2) To make use of a named inner class: define a class within the class definition for the JFrame sub-class e.g., in DemoBasicV3, the class MyListener is defined within the class DemoBasicFrame design the class to implement ActionListener 51... public class NameOfProgramFrame extends JFrame public... private... Declare variables ( fields, attributes ) Inner Class public class NameOfProgramFrame() Constuctor class MyListener implements ActionListener public void actionperformed(actionevent ae... private... public... class must implement all of the ActionListener methods (there is only one, though) Other methods 52

A: Registering an ActionListener B: Using the JFrame sub-class Suppose an ActionListener can be instantiated from a named inner class To register it, invoke the following: togglebutton.addactionlistener( new MyListener() ); In all of these examples, DemoBasicFrame was defined as a subclass of JFrame What prevents us from also making DemoBasicFrame an ActionListener? **This was done in DemoBasicV3 53 54 B: Using the JFrame sub-class How to make DemoBasicFrame an ActionListener: need to assert that it implements the ActionListener interface use keyword implements in class definition need to ensure that method bodies are provided for all of the methods defined in the interface the compiler will ensure this e.g., in DemoBasicV4, the class DemoBasicFrame implements ActionListener import... public class NameOfProgram public static void main(string[] args) public class NameOfProgramFrame extends JFrame implements ActionListener Our GUI class implements the methods of the ActionListener listener 55 56

... public class NameOfProgramFrame extends JFrame implements ActionListener Declare variables ( fields, attributes ) public... private... Constructor public class NameOfProgramFrame() public void actionperformed(actionevent ae) public... private... Other methods Must implement all of the ActionListener methods (there is only one, though) 57 B: Registering an ActionListener Suppose the constructor of the JFrame sub-class also instantiates an ActionListener To register it, invoke the following (from within constructor): togglebutton.addactionlistener(this); **This was done in DemoBasic_v4 58 C: Using an Anonymous Inner Class C: Using an Anonymous Inner Class What is an anonymous inner class? an inner class that is declared without a name Syntax: new <ClassOrInterfaceName>() <body> This can be used in places where an instance is needed 59 Finally, we revisit DemoVeryBasicV3: Recall we added a window listener to DemoVeryBasicFrame, the argument to the addwindowlistener method was: new WindowAdapter() public void windowclosing(windowevent we) System.exit(0); this defines a WindowListener using an anonymous inner class 60

C: Using an Anonymous Inner Class Discussion We can do the same for ActionListener: new ActionListener() public void actionperformed(actionevent ae)... this defines an ActionListener **This was done in DemoBasic_v5 61 To make use of a particular listener: should I extend its adapter class, or should I implement the interface? If I implement the interface, which class should do it? the JFrame child class, a named inner class, or an anonymous inner class? If I extend the adapter, should I do it with an anonymous inner class, or a named inner class? 62 Discussion Discussion When extending adapter classes: need to provide code only for methods that are needed need to define an additional inner class (either named or anonymous) if you use an anonymous inner class, your code can be difficult to read some say use sparingly, only for classes with one or two short methods; some say don t use at all if you use a named inner class, you need to instantiate an additional object you can only extend one adapter class (compare with next option) Java does not include multiple inheritance (unlike C++ or Eiffel) 63 When implementing interfaces: need to provide code for all of the methods whether you need them or not can define method bodies to be empty, though don t need to define an additional inner class; you can use the JFrame subclass a class can implement many different listeners 64

Whether to Extend Adapters or Implement Listeners Largely a matter of personal choice Sample applications in this course will do both in DemoBasicV3, V4, we implemented the ActionListener interface note that no ActionAdapter class is defined in Java DemoVeryBasicV4 extended WindowAdapter this was done using an anonymous inner class you could easily define a named inner class instead (in fact, the code probably would be more readable this way) 65