Component-Based Software Engineering

Slides:



Advertisements
Similar presentations
Graphical User Interfaces Java’s AWT and Swing APIs.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Object-Oriented Programming with Java The Java Event Model Lecture 5.
Event Handling Events and Listeners Timers and Animation.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Writing GUI Applications An introduction with Java.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Graphical User Interfaces
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Internet Software Development The Java Event Model Lecture 5.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Io package as Java’s basic I/O system continue’d.
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 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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:
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Multiple buttons and action calls
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Processing Timer Events
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Timer class and inner classes
Introduction to Event Handling
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Graphical User Interface
Presentation transcript:

Component-Based Software Engineering The Java Event Model Paul Krause

Callbacks and Events Contents Callbacks Events Events in Java

Problems Pauls Pictures Pauls Documents Pauls Sums Pauls Pictures Pauls Homework Pauls Pictures Pauls Documents Pauls Sums Pauls Homework Pauls ToDo Lists

Window Library Client File System But I need to tell you I’m in charge something! I’m in charge here, guys! My Documents Reports Papers Presentations Client File System Slide Shows Calls

Callbacks Callbacks are used in procedural libraries when they need to handle asynchronous events The alternative is for the client to continuously poll the library for events This is inefficient, especially if a large number of library components are registered with a client But the use of callback means a client may observe “intermediate” states of a library “Classically” a library’s operations would run to completion before returning control

Call Sequence Client aUser Library Client installs callback Third party calls library “I need to tell you something!” Library invokes callback “What’s happened?” Callback queries library “He’s changed a name!” “That’s cool” Callback returns Library returns

Directory Service public class Directory { public void addEntry(String name, File file) { // pre name != “” and file != null // post File file = map.get(name) } public void removeEntry(String name) { // pre name != “” // post map.get(name) = nil public void registerNotifier(Notifier n) { // pre n != nil // post n registered, will be called on addEntry and removeEntry

Callbacks and Events Contents Callbacks Events Events in Java

Events An abstraction of Callback that is applicable to “federations” of interacting components The firing of an event is a way of one object telling one or more other recipients that something interesting has happened The sender fires an event A recipient is called a listener and handles the event

Register Event Listener Java Event Model Event Source Register Event Listener Fire Event Event Object Event Listener

Event Objects Encapsulates information specific to an instance of an event E.g. a “mouse click” event may contain: The position of the mouse pointer Which mouse button was clicked (and how many times) The event object is passed as a parameter to the event notification method

Event Listeners These are objects that need to be notified when a certain event occurs Event notifications are made through method invocations in the listening object The event object is passed as a parameter The event source must know which listener object(s) to call This information is contained in an event-listener interface

Event Sources Objects that fire events Implement methods that allow listeners to: Register their interest in the events they generate; Unregister their interest in the events they generate. Multicast event delivery enables an event to be fired to a number of event-listeners

invokes notifications in Summary EventObject source getSource() toString() passed to fires EventSource addListener() removeListener() EventListener notification(evt) registers 0..* invokes notifications in 0..*

Callbacks and Events Contents Callbacks Events Events in Java

Java Events An “Event” encapsulates information about some state change in its source Pressing a button Entering text in a text field Moving a slider on a scroll bar, … Events need not be generated by users: Expiration of a timer Arrival of incoming data, …

Event Sources An event source must: Provide methods to enable “listeners” to register and unregister interest in its events Fire the events, of course! “Send” each event to all listeners who are interested in that type of event For an event of Type, and listener e1: public void addTypeListener(TypeListener e1); removeTypeListener(TypeListener e1);

Listeners Have three responsibilities: Register for events of a certain Type Implement an interface to “receive” events of a certain Type Unregister if they no longer wish to receive events of Type

Events EventObject: AWTEvent: EventObject(Object source) Object getSource( ) String toString( ) AWTEvent: AWTEvent(Object source, int id) int getID( ) Object EventObject AWTEvent

Example: Temperature Converter // Event Source convertTemp = new JButton("Convert..."); … // Listen to events from Convert button. convertTemp.addActionListener(this); // Implementation of ActionListener interface. public void actionPerformed(ActionEvent event) { // Parse degrees Celsius as a double and convert to Fahrenheit. int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); } //v 1.3 // Example taken from the Sun website: // http://java.sun.com/docs/books/tutorial/uiswing/mini/index.html import java.awt.*; import java.awt.event.*; import javax.swing.*; // This example demonstrates the use of JButton, JTextField // and JLabel. public class CelsiusConverter implements ActionListener { JFrame converterFrame; JPanel converterPanel; JTextField tempCelsius; JLabel celsiusLabel, fahrenheitLabel; JButton convertTemp; // Constructor public CelsiusConverter() { // Create the frame and container. converterFrame = new JFrame("Convert Celsius to Fahrenheit"); converterFrame.setSize(40, 40); converterPanel = new JPanel(); converterPanel.setLayout(new GridLayout(2, 2)); // Add the widgets. addWidgets(); // Add the panel to the frame. converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER); // Exit when the window is closed. converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Show the converter. converterFrame.pack(); converterFrame.setVisible(true); } // Create and add the widgets for converter. private void addWidgets() { // Create widgets. tempCelsius = new JTextField(2); celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT); convertTemp = new JButton("Convert..."); fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT); // Listen to events from Convert button. convertTemp.addActionListener(this); // Add widgets to container. converterPanel.add(tempCelsius); converterPanel.add(celsiusLabel); converterPanel.add(convertTemp); converterPanel.add(fahrenheitLabel); celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); // Implementation of ActionListener interface. public void actionPerformed(ActionEvent event) { // Parse degrees Celsius as a double and convert to Fahrenheit. int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // main method public static void main(String[] args) { // Set the look and feel. try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) {} CelsiusConverter converter = new CelsiusConverter();

Summary The components of a software product must interact to achieve a goal In general, it is hard to identify a single component that has overall control So a general model is for components to interact by triggering “Events” that other components register an interest in