Event Handling Feb 14, 2000. Event Model Revisited zRecall that a component can fire an event. zEach type of event is defined as a distinct class. zAn.

Slides:



Advertisements
Similar presentations
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Advertisements

Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
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.
For IST410 Students only Events-1 Event Handling.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
Graphical User Interfaces
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
GUI Basics and Event- Driven Programming Feb 7, 2000.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
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.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
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.
ProgrammingLanguages Programming Languages The Java Event Model In this lesson, we look at the content of these objects and how they are processed in.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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 CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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.
CSI 3125, Preliminaries, page 1 Adapter Class. CSI 3125, Preliminaries, page 2 Adapter Class Java provides a special feature, called an adapter class,
Event Handling and Listeners in SWING The practice of event handling.
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.
Lesson 33: Layout management and drawing – Java GUI.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
GUI Programming in Java Hao Jiang Boston College April, 2009.
Component-Based Software Engineering GUI Basics Paul J Krause.
Multiple buttons and action calls
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
CHAPTER Reacting to the user.
Event Handling Chapter 2 Objectives
Java GUI.
A Quick Java Swing Tutorial
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Web Design & Development Lecture 12
Web Design & Development Lecture 13
The calculator – Java GUI
1 Graphical User Interfaces
A Quick Java Swing Tutorial
Programming Graphical User Interface (GUI)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Event Handling Feb 14, 2000

Event Model Revisited zRecall that a component can fire an event. zEach type of event is defined as a distinct class. zAn event is received by one or more listeners. zAn event listener is an object of a class which implements particular type of listener interface zSince a class can implement multiple interfaces, an object can act on multiple events of different types. zTo receive an event of particular type, we only need to register with the component that fire the event.

Event Hierarchy java.util.EventObject java.awt.AWTEvent java.awt.event.ComponentEvent java.awt.event.FocusEvent java.awt.event.KeyEvent java.awt.event.MouseEvent java.awt.event.AdjustmentEvent java.awt.event.WindowEvent java.awt.event.ActionEvent java.awt.event.ItemEvent java.awt.event.TextEvent

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo implements ActionListener { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! public void actionPerformed( ActionEvent event) { String command = event.getActionCommand(); System.out.println(command); } public static void main(String args[]) { new ButtonDemo(); } private void addButtons() { for (int i=0; i<10; i++) { JButton jb = new JButton(""+i); jb.addActionListener(this); pane.add(jb); } JButton jo = new JButton("+"); jo.addActionListener(this); pane.add(jo); jo = new JButton("-"); jo.addActionListener(this); pane.add(jo); jo = new JButton("*"); jo.addActionListener(this); pane.add(jo); jo = new JButton("/"); jo.addActionListener(this); pane.add(jo); jo = new JButton("AC"); jo.addActionListener(this); pane.add(jo); }

Different Ways to Handle Events zDirectly implements a listener interface of interest. zUse an inner class that implements a listener interface of interest. zUse a listener adapter class, which is an abstract class. (We must override its methods) zQuite often, anonymous class will be useful!!! Anonymous class can be both listener inner class or listener adapter class.

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! // add ButtonListener inner class here. public static void main(String args[]) { new ButtonDemo(); } class ButtonListener implements ActionListener { public void actionPerformed( ActionEvent event) { String command = event.getActionCommand(); System.out.println(command); } private void addButtons() { ButtonListener blt = new ButtonListener(); … … … Jbutton jo = new JButton("AC"); jo.addActionListener(blt); pane.add(jo); }

Listener Adapters zUsually we are not interested in all the methods defined in some listener interface. zHowever, to use an interface we need to implement all the methods in it. (Annoying!!!) zAdapters are abstract classes that already implements particular listener interface. zActually no method in adapter class is an abstract method. Adapter class is just defined as abstract to force user to override particular method of interest.

void windowActivated(WindowEvent ev); void windowClosing(WindowEvent ev); void windowDeactivated(WindowEvent ev); … … … void windowOpened(WindowEvent ev); WindowListener Interface void windowActivated(WindowEvent ev) {} void windowClosing(WindowEvent ev){} void windowDeactivated(WindowEventev){} … … … void windowOpened(WindowEvent ev){} WindowAdapter class implements extends void windowClosing(WindowEent ev) { System.exit(0); } Overrides only one method of interest

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo { Container pane; ButtonDemo() { JFrame frame = new JFrame("Button Demo"); pane = frame.getContentPane(); pane.setLayout(new GridLayout(5,3)); addButtons(); Closer closer = new Closer(); frame.addWindowListener(closer); frame.pack(); frame.setSize(300,200); frame.setVisible(true); } // place addButtons() here!!! // add ButtonListener inner class here. // add Closer class here!!! public static void main(String args[]) { new ButtonDemo(); } class Closer extend WindowAdapter { public void windowClosing( WindowEvent ev) { System.exit(0); }

Shortcut to Use Adapters (Anonymous Class) … …... public class ButtonDemo { Container pane; ButtonDemo() { … … … frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } ) …... } … …... } No object handle is defined!!!

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintDemo { PaintDemo(){ JFrame frame = new JFrame("PaintDemo"); frame.setSize(400,300); DrawingCanvas canvas = new DrawingCanvas(); Container pane = frame.getContentPane(); pane.add(canvas,"Center"); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent evt) { System.exit(0); } ); frame.setVisible(true); } // PaintDemo Continues public static void main(String args[]) { new PaintDemo(); } } // End of PaintDemo

class DrawingCanvas extends JPanel { int last_x,last_y; int cur_x,cur_y; DrawingCanvas() { setSize(getPreferredSize()); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent evt) { last_x = evt.getX(); last_y = evt.getY(); } ); addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { cur_x = evt.getX(); cur_y = evt.getY(); Graphics g = getGraphics() g.drawLine(last_x,last_y, cur_x,cur_y); last_x = cur_x; last_y = cur_y; } ); }