Event Driven Programming in Java SequentialProcedural Programming q

















![public static void main(String[] args) { JFrame frm = new JFrame("Flow Layout Test"); Container public static void main(String[] args) { JFrame frm = new JFrame("Flow Layout Test"); Container](https://slidetodoc.com/presentation_image_h2/446b710cdd93dfc46cff7c9fd24f99e5/image-18.jpg)

- Slides: 19

Event Driven Programming in Java

Sequential/Procedural Programming q Program runs as programmer intended q Programmer sets the order of action q Programmer controls the program’s flow

Event Driven Programming q User-centric q Computer user determines the order of actions q Programs are interactive q Flow of control is determined at runtime § User clicks mouse / presses key

Event Handling in Java q Basic Elements: § Event source § Event listener (or handler)

Event q An event means something that happens q Can be caused by computer or user § Mouse Click § Key Press § System Clock Tick q Can occur differently each time the program is executed q A way for GUI components to communicate with the rest of the program. q In Java, events are implemented as event classes (e. g. , Action. Event)

Some most common event classes Event. Object AWTEvent Action. Event Component. Event Input. Event To be used in this class Mouse. Event Window. Event Key. Event

Event Source q An event source is a GUI object where an event occurs. We say an event source generates events. q Buttons, text boxes, list boxes, and menus are common event sources in GUI-based applications. q Although possible, we do not, under normal circumstances, define our own event sources when writing GUI-based applications.

Event listener (or handler) q q Objects that receives and processes events Must implement an appropriate listener interface Must be registered with the event sources. May listen to several sources and different types of events

Types of Event Listeners Act that results in event Listener type User clicks a button, presses Return while typing in a text field, or chooses a menu item Action. Listener User closes a frame (main window) Window. Listener User presses a mouse button while the cursor is over a component Mouse. Listener User moves the mouse over a component Mouse. Motion. Listener Component becomes visible Component. Listener Component gets the keyboard focus Focus. Listener Table or list selection changes List. Selection. Listener

Action. Listener Interface q When we call the add. Action. Listener method of an event source, we must pass an instance of a class that implements the Action. Listener interface. q The Action. Listener interface includes one method named action. Performed. q A class that implements the Action. Listener interface must therefore provide the method body of action. Performed. q Since action. Performed is the method that will be called when an action event is generated, this is the place where we put a code we want to be executed in response to the generated events.

To implement an action listener // Action listener class program. Class implements Action. Listener { public void action. Performed(Action. Event e) { // handle the event e … } }

Connecting Source and Listener event source JButton event source event listener notify register Handler notify JText. Field register q A listener must be registered with event sources. Once registered, it will get notified when the event sources generates events. q A listener can be registered with more than one event source.

To create and register an action listener // create an action listener Action. Listener AL = new program. Class() ; // create a button JButton = new JButton(“button. Text”); // register the action listener with button. add. Action. Listener(AL); // create a text field JText. Field tf = new JText. Field(); // register the action listener with the text field tf. add. Action. Listener(AL);

To identify the event source There are two methods: 1. public Object get. Source() Returns the object on which the Event actually occurred. 2. public String get. Action. Command() Returns the command string associated with this action. 3. Public void set. Action. Command(String action. Command) Sets the action command for this button.

To handle events 1 // Action listener class program. Class implements Action. Listener { public void action. Performed(Action. Event e) { if (e. get. Source() == Button) System. out. println(“Button pressed!”); else if (e. get. Source() == tf) System. out. println(“Text is entered !”); } }

To handle events 2 // Action listener class program. Class implements Action. Listener { public void action. Performed(Action. Event e) { if (e. get. Action. Command(). equals(“button. Text”)) System. out. println(“Button pressed!”); else if (e. get. Source() == tf) System. out. println(“Text is entered !”); } }

import javax. swing. *; import java. awt. event. *; public class action. Test implements Action. Listener{ static JButton[] Button = new JButton[10]; static JText. Field tf; A complete example public void action. Performed(Action. Event e) { for (int i=0; i<=9; ++i) { if(e. get. Source() == Button[i]) { System. out. printf("Button %d is pressed!n", i); return; } } if (e. get. Source() == tf) System. out. println("Text field contains “+tf. get. Text()); }
![public static void mainString args JFrame frm new JFrameFlow Layout Test Container public static void main(String[] args) { JFrame frm = new JFrame("Flow Layout Test"); Container](https://slidetodoc.com/presentation_image_h2/446b710cdd93dfc46cff7c9fd24f99e5/image-18.jpg)
public static void main(String[] args) { JFrame frm = new JFrame("Flow Layout Test"); Container content. Pane = frm. get. Content. Pane(); content. Pane. set. Layout(new Flow. Layout()); Action. Listener AL = new action. Test() ; for (int i=0; i<=9; ++i) { Button[i] = new JButton(""+i); content. Pane. add(Button[i]); Button[i]. add. Action. Listener(AL); } tf = new JText. Field("", 27); tf. set. Horizontal. Alignment(JText. Field. RIGHT); tf. add. Action. Listener(AL); content. Pane. add(tf); frm. pack(); frm. set. Size(318, 220); //frm. set. Resizable(false); frm. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frm. set. Visible(true); } A complete example conti. }

import javax. swing. *; import java. awt. event. *; public class action. Test implements Action. Listener{ static JButton[] Button = new JButton[10]; static JText. Field tf; A complete example public void action. Performed(Action. Event e) { if (e. get. Source() == tf) System. out. println(“Text field contains “+tf. get. Text()); else System. out. println(“Button “+ e. get. Action. Command()+” is pressed!n”); } }