Week 8 Building Graphical User Interfaces in Java Steps to create a GUI Swing GUI components Event handling Software Design Pattern Observer, Composite,

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Introduction to Java 2 Programming
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Design Patterns and Graphical User Interfaces Horstmann ,
Welcome to CIS 083 ! Events CIS 068.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
UID – Event Handling and Listeners Boriana Koleva
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Graphical User Interfaces
CompSci 230 S Programming Techniques
Review: Java GUI Programming
Ellen Walker Hiram College
Event Driven Programming and Graphical User Interface
Introduction to Computing Using Java
Introduction to Event Handling
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Week 8 Building Graphical User Interfaces in Java Steps to create a GUI Swing GUI components Event handling Software Design Pattern Observer, Composite, Strategy, Decorator 1 Object-oriented Software Design

2 An example of Swing application l You have used some Swing components in previous exercises firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); JOptionPane.showMessageDialog(null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); l Swing components examples nRun the applications and take a closer look at what are the GUI components in there.

3 Event-driven programming Event-driven programming vs. imperative programming nAn ‘imperative’ program interacts with the data (primitives and objects) through control structures such as IF...THEN, WHILE..., SWITCH..., FOR..., etc. The entire program's flow is defined by these structures before hand. nIn even-driven programming: instead of tightly controlling the run of a program, the software just sits idle until the user does something. In other words, the flow of the program is controlled by user-generated events. oThe meat of event-driven programming is in the event-handlers. These are code in the software that are called in response to certain events oThe most important conceptual feature of even-driven programming is that the user is in control of your code.

4 Four basic steps to create a GUI (1/2) l Check one of the examples to identify the steps below. l (1) Create and configure the components nusing their constructors as they are all objects in Java nsetting their properties l (2) Add the components to a container nComponents must be placed in a container oTop-level containers are: JApplet, JFrame and JDialog oImportant one: JPanel can be placed in other container e.g. JFrame

5 Four basic steps to create a GUI (2/2) l (3) Arrange, or layout, the components nto set positions and sizes nlayout manager objects (again, they are objects in Java) nA layout manager object is then used to set a layout of a container e.g. o northPanel.setLayout( new BorderLayout() ); nthere exist several layout manager objects in Java l (4) handle the events generated by the components nevent object contains info about user interaction nevent listener object is notified and respond in a way that was coded.

6 An overview of Swing components Source: The Sun Java Tutorial at l Top-Level Containers nThe components at the top of any Swing containment hierarchy. l General-Purpose Containers nIntermediate containers that can be used under many different circumstances. l Special-Purpose Containers nIntermediate containers that play specific roles in the UI. l Basic Controls nAtomic components that exist primarily to get input from the user; they generally also show simple state. l Uneditable Information Displays nAtomic components that exist solely to give the user information. l Editable Displays of Formatted Information nAtomic components that display highly formatted information that (if you choose) can be edited by the user. l NB: the containment hierarchy of your GUI

7 Event handling in Java (1/3) l Every time the user interacts with a GUI, an event occurs l Any object can be notified of the event l Response to the event is coded in an event handler.

8 Event handling in Java (2/3) l Event source: any GUI component such as buttons, Textfield, Label, listbox, combobox (drop-down box) etc. l Event type: they are classes in Java nKeyEvent, MouseEvent, ActionEvent, WindowEvent, FocusEvent, MenuEvent, CareEvent l Event listener: those objects want to capture an event and respond to it They must be registered with the event’s source e.g. someComponent.addActionListener(instanceOfMyClass); l Event handler: methods of a class / an object that implements an event listener’s interface or override methods of an event adapter’s class nE.g. WindowListener is an interface that specifies the methods of the WindowEvent Listener, while WindowAdapter is a class that implements dummy methods of the WindowListener interface

9 Event handling in Java (3/3) l Methods to implement an event handler nTo implement a listener interface, all methods must be implemented opublic class myClass implements ActionListener  BTW, ActionListener interface has only 1 method actionPerformed nTo extend an event adapter class: overriding only required methods public class myClass extends WindowAdapter { public void WindowClosing(WindowEvent e) { System.exit(0); } } nTo use inner class: a class defined within another class oSee examples in FiveGUIcompsExamples (downloadable from w8 notes) l Implementing Listeners for Commonly Handled Events nhttp://java.sun.com/docs/books/tutorial/uiswing/events/handling.html

10 Software pattern l What is it? nA software design pattern is a description of a problem and its solution that may be applied to many programming situations. nBenefits oSave time and effort oHave proven (possiblly optimal or best) solution oAvoid possible errors (anti-pattern) l Originated from practice nThe book by Gang of Four oDesign Patterns by Gamma et al. oA Google search returns a number of websites

11 Observer pattern: Intent l Model/View/Controller nModel: data structure, no visual representation nViews: visual representations nControllers: user interaction l Observer Pattern nModel notifies views when something interesting happens nButton notifies action listeners when something interesting happens nViews attach themselves to model in order to be notified nAction listeners attach themselves to button in order to be notified nGeneralize: Observers attach themselves to subject. *Illustration by Horstmann

12 Observer Pattern: Description l Context 1. An object, called the subject, is source of events 2. One or more observer objects want to be notified when such an event occurs. l Solution 1. Define an observer interface type. All concrete observers implement it. 2. The subject maintains a collection of observers. 3. The subject supplies methods for attaching and detaching observers. 4. Whenever an event occurs, the subject notifies all observers. *Illustration by Horstmann

13 Names in Observer Pattern: Example Name in Design Pattern Actual Name (Swing buttons) SubjectJButton ObserverActionListener ConcreteObserver The class that implements the ActionListener interface type attach()addActionListener() notify()actionPerformed() l See the JButton Java example

14 Other patterns Please read Chapter 5, Horstmann book l Strategy Pattern nE.g. Layout managers l Composite Pattern nE.g. AWT Components, Swing JPanel l Decorator Pattern nE.g. Scrollbar l Iterator nThe list Iterator in Java

15 How to Recognize Patterns? l Look at the intent of the pattern nE.g. COMPOSITE has different intent than DECORATOR l Remember common uses (e.g. STRATEGY for layout managers) l Not everything that is strategic is an example of STRATEGY pattern l Use context and solution as "litmus test“ nTest every point of the pattern’s description

16 Summary l An overview of building GUIs in Java nSteps to create a GUI oComponents oLayout manager oContainment hierarchy nEvent handling in Java oThe event handling mechanism oMethods to implement an event handler l Design pattern nConcept nSome examples