Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
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.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
3461 Sequential vs. Event-driven Programming Reacting to the user.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
– 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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
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.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
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 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 14 Event-Driven Programming
Welcome To java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Aum Amriteshwaryai Namah
Handling User Events with Swing
Event Handling Chapter 2 Objectives
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
Event-driven programming for GUI
Unit I: Chapter 1 Event handling.
CHAPTER 2 Event Handling.
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
Web Design & Development Lecture 12
Web Design & Development Lecture 13
Presentation transcript:

Pravin Yannawar, DOCS, NMU Jalgaon

Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate it using example List the categories of Events & Listener interfaces Demonstrate the above example using an independent event handler class Identify the need for adapter classes & demonstrate it using an independent event handler example

Basic Java : Event handling in AWT and Swing 3 Event Handling GUI applications are event-driven applications. They generate events when the user of the program interacts with the GUI. The underlying OS is constantly monitoring these events. When a event occurs, the OS reports these events to the programs that are running. The application will handle the event using a appropriate “Event Handler”.

Basic Java : Event handling in AWT and Swing 4 Delegation Event Model Source generates an event & sends it to one or more listeners. Listener simply waits until it receives an event. Once received, listener processes the event & returns.  Advantage : application logic that processes event is clearly separated from the UI logic that generates those events.  An UI element is able to delegate the processing of an event to a separate piece of code ( Event handler)

Basic Java : Event handling in AWT and Swing 5 Delegation Event Model Event: is an object that describes a state change in a source. Event Source: is an object that generates an event.  A source must register listeners for listeners to receive notifications about a specific type of event. Event Listener : is an object that is notified when an event occurs.  It must be registered with a source.  It must implement methods to receive & process these notifications.

Basic Java : Event handling in AWT and Swing 6 Event Handling import java.awt.*; import java.awt.event.*; class MyFrame extends Frame implements ActionListener { TextField t1; Button b1; MyFrame() { t1 = new TextField(20); b1 = new Button(“Click”); add(t1,”North”); b1.addActionListener(this); add(b1,”South”); }

Basic Java : Event handling in AWT and Swing 7 Event Handling public void actionPerformed( ActionEvent e ) { t1.setText(“Click pressed”); } public static void main(String args[]) { Frame f = new MyFrame(); f.setSize(100,100); f.show(); }

Basic Java : Event handling in AWT and Swing 8 Inheritance diagram of the AWT Event Classes Event Object AWT Event Component Event Action Event Adjustment Event Item Event Text Event Input Event Container Event Focus Event Paint Event Window Event Mouse Event Key Event

Basic Java : Event handling in AWT and Swing 9 Event Listener Interfaces ActionListener AdjustmentListener ComponentListener FocusListener ItemListener MouseListener MouseMotionListener KeyListener TextListener WindowListener ContainerListener java.util.EventListener

Basic Java : Event handling in AWT and Swing 10 Event Handling Summary InterfaceMethodsParameterEvents Generated by ActionListneractionPerformedActionEvent getActionCommand getmodifiers Button List MenuItem TextField Adjustment Listner adjustmentValue Changed AdjustmentEvent getAdjustable getAdjustmentType getvalue Scrollbar ItemListneritemStateChangedItemEvent getItem getItemSelectable getstateChange Checkbox CheckboxMenu Item Choice List TextListnertextValue ChangedTextEventTextComponent

Basic Java : Event handling in AWT and Swing 11 Event Handling Summary InterfaceMethodsParameterEvents Generated by Component Listener componentMoved componentHidden componentResized ComponentEvent getComponent Component Container Listener componentAdded componentRemoved ContainerEvent getChild getContainer Container FocusListenerfocusGained focusLost focusEvent IsTemporary Component KeyListenerkeyPressed keyRealsed keyTyped KeyEvent getKeyChar getKeyCode getkeyModifiersText isActionKey Component

Basic Java : Event handling in AWT and Swing 12 Event Handling Summary InterfaceMethodsParameterEvents Generated by Mouse Listener mousePressed mouseRealesed mouseEntered mouseExited mouseClicked MouseEvent getClickCount getX getY getPoint translatePoint isPopupTrigger Component MouseMotion Listener mouseDragged mouseMoved Component Window Listener windowClosing windowOpened windowIconed windowDeiconed windowClosed windowActivated windowDeactivated WindowEvent getWindow Window

Basic Java : Event handling in AWT and Swing 13 Delegation Event Model with Another Class import java.awt.*; class MyFrame extends Frame { TextField t1; Button b1; MyFrame() { t1 = new TextField(20); b1 = new Button(“Click”); add(t1,”North”); b1.addActionListener(new ButtonHandler(this)); add(b1,”South”); }

Basic Java : Event handling in AWT and Swing 14 Delegation Event Model with Another Class class ButtonHandler implements ActionListener { MyFrame f ; ButtonHandler(MyFrame mf) { f=mf; } public void actionPerformed( ActionEvent e ) { // code for processing button press. }

Basic Java : Event handling in AWT and Swing 15 Adapter Classes Many of the listener interfaces have more than one method. Thus, if a particular interface is implemented, all the methods of that interface should also be implemented. To simplify this task, listener interfaces with more than one method come with adapter classes. Adapter classes implement all the methods of an interface. You can extend the adapter class to specify the desired reaction to some methods.

Basic Java : Event handling in AWT and Swing 16 Without Adapter Classes class MyFrame extends Frame implements WindowListener { public void windowActivated(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowOpened(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowClosing(WindowEvent e){ System.exit(0); }

Basic Java : Event handling in AWT and Swing 17 Without Adapter Class public static void main(String args[]) { Frame f = new MyFrame(); f.setSize(100,100); f.show(); f.addWindowListener(f); }

Basic Java : Event handling in AWT and Swing 18 Using Adapter Class import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { MyFrame() { addWindowListener(new WindowHandler()); }

Basic Java : Event handling in AWT and Swing 19 Adapter Classes class WindowHandler extends WindowAdapter { { public void windowClosing(WindowEvent e) { System.exit(0); }