Web Design & Development Lecture 11

Slides:



Advertisements
Similar presentations
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Advertisements

Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
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.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
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.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
A simple swing example GETTING STARTED WITH WIND CHILL.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
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.
GUIs & Event-Driven Programming Chapter 11 Review.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
GUI Programming using Java - Event Handling
Lecture 15 Basic GUI programming
Multiple buttons and action calls
Events and Event Handling
Graphical User Interfaces
Chapter 14 Event-Driven Programming
Welcome To java
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
A First Look at GUI Applications
Processing Timer Events
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 -->
GUI Programming III: Events
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Event Driven Programming and Graphical User Interface
Introduction to Event Handling
Web Design & Development Lecture 12
Web Design & Development Lecture 13
Constructors, GUI’s(Using Swing) and ActionListner
Tonga Institute of Higher Education
Presentation transcript:

Web Design & Development Lecture 11

Event Handling

GUI are Event Driven

Events GUIs generate events when the user interacts with GUI For example, Clicking a button Moving the mouse Closing Window etc In java, events are represented by Objects These objects tells us about event and its source. Examples are ActionEvent (Clicking a button) WindowEvent (Doing something with window e.g. closing , minimizing) Both AWT and swing components (not all) generate events java.awt.event.*; javax.swing.event.*;

Some event classes of java.awt.event

Event Handling Model Common for both AWT and Swing components Event Delegation Model Processing of an event is delegated to a particular object (handlers ) in the program Publish-Subscribe Separate UI code from program logic

Event Handling Steps For a programmer the event Handling is a three step process in terms of code Step 1 Create components which can generate events Step 2 Build component (objects) that can handle events (Event Handlers) Step 3 Register handlers with generators

Event Handling Process [1] Event Generators You have already seen alot of event generators Buttons Mouse Key Window Etc JButton b1 = new JButton(“Hello”); Now b1 can generate events

Event Handling Process [2] Event Handlers/ Event Listener First Technique - By Implementing Listener Interfaces Java defines interfaces for every event type If a class needs to handle an event. It needs to implement the corresponding listener interface To handle “ActionEvent” a class needs to implement “ActionListener” To handle “KeyEvent” a class needs to implement “KeyListener” To handle “MouseEvent” a class needs to implement “MouseListener” And so on

Event Listener interfaces of package java.awt.event

Example Listeners

Event Handling Process [3] Event Handlers By implementing an interface the class agrees to implement all the methods that are present in that interface. Implementing an interface is like signing a contract Inside the method the class can do what ever it wants to do with that event Event Generator and Event Handler can be the same or different classes

Event Handling Process [4] Event Handlers To handle events generated by Button. A class needs to implement ActionListener interface. public class Test implements ActionListener{ public void actionPerformed(ActionEvent ae){ // do something }

Event Handling Process [4] Registering Handler with Generator The event generator is told about the object which can handle its events Event Generators have a method add______Listener(________) b1.addActionListener(objectOfTestClass)

Event Handling Simple Example

Event Handling: Simple Example Scenario When Hello button is pressed, the Dialog box would be displayed

Event Handling: Simple Example Step 1(cont.) /* This program demonstrates the handling of Action Event. Whenever “Hello” button is presses, a dialog box would be displayed in response containing some informative message */ import java.awt.*; import javax.swing.*; impor java.awt.event.*; public class ActionEventTest { JFrame frame; JButton bHello;

Event Handling: Simple Example Step 1 (cont.) public void initGUI () { frame = new JFrame(); // Event Generator bHello = new JButton(“Hello”); Container con = frame.getContenetPane(); con.add(bHello); frame.setSize(200,200); frame.setVisible(true); }//end initGUI

Event Handling: Simple Example (cont.) Step 2 // import your packages public class ActionEventTest implements ActionListener { ……. public void initGUI() …. public void actionPerformed (ActionEvent ae ){ JOptionPane.showMessageDialog(“Hello is pressed”); }

Event Handling: Simple Example Step 3 (cont.) public void initGUI () { // Event Generator bHello = new JButton(“Hello”); Container con = frame.getContenetPane(); con.add(bHello); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setVisible(true); // Event Registration bHello.addActionListner(this); }//end initGUI

Event Handling: Simple Example (cont.) public ActionEventTest( ) { initGUI (); } public static void main (String args [ ]) { ActionEventTest aeTest = new ActionEventTest(); }//end ActionEvent class

Event Handling: Simple Example Complete Code import java.awt.*; import javax.swing.*; impor java.awt.event.*; public class ActionEventTest implements ActionListner{ JFrame frame; JButton bHello; public void initGUI () { frame = new JFrame(); // Event Generator bHello = new JButton(“Hello”); Container con = frame.getContenetPane(); con.add(bHello); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setVisible(true); // Event Registration bHello.addActionListner(this); }//end initGUI public void actionPerformed (ActionEvent ae ){ JOptionPane.showMessageDialog(“Hello is pressed”); } public ActionEventTest( ) { initGUI (); public static void main (String args [ ]) { ActionEventTest aeTest = new ActionEventTest(); }//end ActionEvent class

Behind the Scenes

Event Handling Participants Event Generator / Source Swing and awt components For example, JButton, JTextField, JFrame etc Generates an event object Registers listeners with itself Event Object Encapsulate information about event that occurred and the source of that event For example, if you click a button, ActionEvent object is created

Event Handling Participants (cont.) Event Listener/handler Receives event objects when notified, then responds Each event source can have multiple listeners registered on it Conversely, a single listener can register with multiple event sources

Event Handling Participants (cont.) JVM Receives an event whenever one is generated Looks for the listener/handler of that event If exist, delegate it for processing If not, discard it (event).

Event Handling Diagram Event Queue (managed by JVM) Add event in queue JVM DeQueue Event Pass ActionEvent to Handler Generate ActionEvent Register with Action Hanlder button Action Handler Action Handler Mouse Handler

Making Small Calculator Example Code

Event Handling: Small Calculator Scenario User enters numbers in the provided fields On pressing “+” button, sum would be displayed in the answer field On pressing “*” button, product would be displayed in the answer field

Code: Small Calculator import java.awt.*; import javax.swing.*; impor java.awt.event.*; public class SmallCalcApp implements ActionListener { JFrame frame; JLabel firstOperand, secondOperand, answer; JTextField op1, op2, ans; JButton plus, mul; public void initGUI () { ……. plus = new JButton(“+”); mul = new JButton(“*”); ……. plus.addActionListner(this); mul.addActionListner(this); ……… }//end initGUI

Code: Small Calculator (cont.) // providing definition of interface ActionListner’s methos public void actionPerformed (ActionEvent event ) { String oper, result; int num1, num2, res; if (event.getSource() == plus) { oper = op1.getText(); num1 = Integer.parseInt(oper); oper = op2.getText(); num2 = Integer.parseInt (oper); res = num1+num2; result = res+""; ans.setText(result); } // end if //continue

Code: Small Calculator (cont.) else if (event.getSource() == mul) { oper = op1.getText(); num1 = Integer.parseInt(oper); oper = op2.getText(); num2 = Integer.parseInt (oper); res = num1*num2; result = res+""; ans.setText(result); } } // end actionPerformed method ………………..

Code: Small Calculator (cont.) ……… //write default constructor and call initGUI public static void main (String args[ ]) { SmallCalcApp scApp = new SmallCalcApp(); } } // end SmallCalcApp